有很多关于将 Parse 服务器迁移到不同平台的手册,但没有一个手册说明在云上使用 Expressjs 的 Web 应用程序需要哪些插件和包。有人对这个问题有什么建议吗?
我在装有 Ubuntu 实例的 Amazon AWS 服务器上执行了以下步骤。API 一切正常,但 Web 应用程序 Expressjs 却不行。
节点安装
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs git
sudo apt-get update
sudo npm install -g parse-server pm2
sudo npm install -g [email protected] underscore parse-express-https-redirect
sudo npm install -g parse-express-raw-body [email protected] parse-express-cookie-session
第二步,我尝试使用 node 的 pm2 服务运行 parse-server,如下所示:
使用 Nodejs 创建 pm2 服务器
sudo useradd --create-home --system parse
sudo passwd parse
USER_PASS
sudo su parse
cd ~
mkdir -p ~/cloud
cp -r /tmp/my-cloud-files /home/parse/
\__ cloud
\__ main.js
\__ app.js
\__ ...
\__ public
\__ config
nano ecosystem.json
{
"apps" : [{
"name" : "parse-wrapper",
"script" : "/usr/bin/parse-server",
"watch" : true,
"merge_logs" : true,
"cwd" : "/home/parse",
"env": {
"PARSE_SERVER_CLOUD_CODE_MAIN": "/home/parse/cloud/main.js",
"PARSE_SERVER_DATABASE_URI": "mongodb://mongo_user:mongo_user_pass@localhost:27017/takimtakip",
"PARSE_SERVER_APPLICATION_ID": "application_id",
"PARSE_SERVER_MASTER_KEY": "application_master_key",
}
}]
}
export NODE_PATH= (echo $NODE_PATH):.
pm2 start ecosystem.json
pm2 list
但是当我查看 pm2 日志文件时,我看到以下内容
pm2 show parse-wrapper
┌───────────────────┬────────────────────────────────────────────────┐
│ status │ online │
│ name │ parse-wrapper │
│ restarts │ 72402 │
│ uptime │ 0s │
│ script path │ /usr/bin/parse-server │
│ script args │ N/A │
│ error log path │ /home/parse2/.pm2/logs/parse-wrapper-error.log │
│ out log path │ /home/parse2/.pm2/logs/parse-wrapper-out.log │
│ pid path │ /home/parse2/.pm2/pids/parse-wrapper.pid │
│ interpreter │ node │
│ interpreter args │ N/A │
│ script id │ 0 │
│ exec cwd │ /home/parse │
│ exec mode │ fork_mode │
│ node.js version │ 5.10.1 │
│ watch & reload │ ✘ │
│ unstable restarts │ 0 │
│ created at │ 2016-04-15T14:42:09.280Z │
└───────────────────┴────────────────────────────────────────────────┘
tail -f /home/parse2/.pm2/logs/parse-wrapper-error.log
TypeError: Cannot read property 'defaults' of undefined
at module.exports (/home/parse2/node_modules/parse-express-cookie-session/index.js:64:26)
at Object.<anonymous> (/home/parse2/cloud/app.js:25:9)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/parse2/cloud/main.js:1:63)
答案1
答案2
试试这个方法! 代码块已通过注释进行解释。
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var app = express();
var fs = require('fs');
var api = new ParseServer({
databaseURI: 'mongodb://user:pass@localhost:27017/parse', // Connection string for your MongoDB database
appId: 'your app id',
masterKey: 'your app master key', // Keep this key secret!
serverURL: 'https://localhost:1337/parse' // Don't forget to change to https if needed
});
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
};
// Serve the Parse API on the /parse URL prefix
app.use('/parse', api);
var httpsServer = require('https').createServer(options,app);
httpsServer.listen(1337, function() {
console.log('parse-server-example running on port 1337.');
});