我遇到了试图在Elastic Beanstalk上运行我的NodeJS应用程序的502 Bad Gateway问题。我尝试按照stackoverflow回答告诉我将我的起始脚本从server.js
更改为main.js
,但这没有用。我使用在.env
文件中设置的env变量,这些变量不是我的git存储库的一部分,这可能会导致此错误,还是因为启动脚本而导致错误?
我的配置仪表板中的此部分是否应更改为我的开始文件?
以下是我的应用设置:
框架:Expressjs
数据库:MongoDB
Elastic Beanstalk Server:运行Node.js的64位Amazon Linux 2014.09 v1.2.0
我检查了我的日志,但在我的/var/log/nginx/error.log
中找不到任何内容,我发现的唯一错误部分只包含可能导致错误的信息。
{"status":"SUCCESS","api_version":"1.0","truncated":"false","results":[{"status":"SUCCESS","msg":"","returncode":0,"events":[{"msg":"No start scripts located in package.json. Node.js may have issues starting. Add start scripts or place code in a file named server.js or app.js.","severity":"ERROR","timestamp":1425234947517}]}]}
这是我的package.json文件:
{
"name": "expressjs-blog",
"main": "main.js",
"dependencies": {
"body-parser": "1.6.5",
"ejs": "^1.0.0",
"express": "^4.6.1",
"express-paginate": "0.0.2",
"mongoose": "~3.6.15",
"mongoose-paginate": "^3.1.0",
"serve-favicon": "*",
"passport" : "~0.1.17",
"passport-local" : "~0.1.6",
"connect-flash" : "~0.1.1",
"bcrypt-nodejs" : "latest",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.0",
"method-override": "~1.0.0",
"express-session": "~1.0.0",
"aws-sdk": "*"
}
}
这是我的main.js
文件:
//Load express
var express = require('express');
var app = express();
var router = express.Router(); // get an instance of the router
var bodyParser = require('body-parser'); // configure app to use bodyParser()
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var aws = require('aws-sdk');
app.use(bodyParser.urlencoded({ extended: true})); // get data from a POST method
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(cookieParser());
var port = process.env.PORT || 8080; // set the port
var DB_CONFIG = process.env.DB_CONFIGURATION;
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
var S3_BUCKET = process.env.S3_BUCKET;
var blogDB = require('./config/blogDB.js');
mongoose.connect(blogDB.url);
require('./config/passport.js')(passport);
app.set('view engine', 'ejs'); // set ejs as the view engine
app.use(express.static(__dirname + '/public')); // set the public directory
app.use(session({ secret: 'thisisatest' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
var routes = require('./app/routes');
app.use(routes); // use routes.js
app.listen(port);
console.log('magic is happening on port' + port);
-----------的更新 ------------
我将env变量添加到Amazon配置仪表板中,这将允许它查看连接到数据库和其他变量的位置。我现在收到一个错误日志:
/var/log/nginx/error.log
:
2015/03/04 17:12:03 [error] 15978#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xxx.xx, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "xxxxx-d-xxxxxx-env-xxxxx.elasticbeanstalk.com"
2015/03/04 17:12:03 [error] 15978#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xxx.xx, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "xxxxx-d-xxxxx-env-xxxxxx.elasticbeanstalk.com"
2015/03/04 17:16:27 [error] 15978#0: *4 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xx.xx, server: , request: "GET /phpMyAdmin/translators.html HTTP/1.1", upstream: "http://127.0.0.1:8081/phpMyAdmin/translators.html", host: "xx.xxx.xx.xx"
2015/03/04 17:20:02 [error] 15978#0: *6 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "xxx-d-philxxxxlips-env-rqsrxxe92stf.elasticbeanstalk.com"
2015/03/04 17:20:03 [error] 15978#0: *6 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xxx.xx, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "xxxx-d-xxx-env-rqsre92xxxstf.elasticbeanstalk.com"
答案 0 :(得分:1)
在本地运行npm start
时会发生什么?错误消息表明您尚未指定启动脚本。 AWS可能会使用npm start
来启动您的服务器,并且该命令似乎失败了。
尝试将此添加到package.json:
"scripts" : {
"start" : "node main.js"
}
答案 1 :(得分:1)
固定!我所要做的就是进入配置>容器选项和设置节点命令到node main.js