我有一个有效的node.js应用程序,我想部署到AWS EB。它是有效的,因为它通过以下方式在我的localhost上工作:
npm start
我上传的zip就像这样:
我的app.js看起来像:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var sequelize = require('sequelize');
var routes = require('./routes/index');
var users = require('./routes/users');
var responseDTO = require('./dto/httpResponseDTO.js');
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
app.use(function(err, req, res, next) {
res.status(err.status || 500);
msg = responseDTO.toReseponseDTO(false, err.name, err.errors);
res.json(msg);
});
module.exports = app;
修改
package.json看起来像:
{
"name" : #projectname#,
"version" : "0.1.1",
"private" : true,
"scripts" : {
"start" : "node ./bin/www"
},
"dependencies" : {
...
}
}
脚本尝试运行的文件(./bin/www)如下所示:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('mee:server');
var http = require('http');
var models = require('../models');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '8081');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
看起来这给EB造成了问题,其中说:
Impaired services on all instances.
错误日志显示:
Server running at http://127.0.0.1:8081/
Server running at http://127.0.0.1:8081/
npm ERR! Linux 3.14.48-33.39.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/npm" "start"
npm ERR! node v0.12.6
npm ERR! npm v2.11.2
npm ERR! path /var/app/current/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! enoent ENOENT, open '/var/app/current/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /var/app/current/npm-debug.log
npm ERR! Linux 3.14.48-33.39.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/npm" "start"
npm ERR! node v0.12.6
npm ERR! npm v2.11.2
npm ERR! path /var/app/current/package.json
npm ERR! code ENOENT
npm ERR! errno -2
看起来没有找到package.json?但是从那里的文件夹结构......
请让我知道出了什么问题,很长一段时间一直困扰着我!
答案 0 :(得分:0)
从您发布的代码中我看不到实际启动服务器时的部分但是从日志中我看到它从端口8081开始,这对我来说实际上很奇怪,我想在EBS中它必须有一个动态PORT < / p>
当你选择端口时,你应该使用process.env.PORT || 8081
这将获得EBS设置的端口或使用8081(例如在本地)。
亚马逊有一个很好的指南,可以解释如何在那里部署节点应用程序:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html
答案 1 :(得分:0)
由于某些原因,当我远程进入EB使用的linux ec2实例,并尝试使用node --version或sudo node --version时,它说找不到命令。我必须手动安装node和npm并手动启动服务器。我必须做到这一点很奇怪..