gpt4 book ai didi

node.js - 无法将我自己的 Node js部署到aws elastic beanstalk

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:33 25 4
gpt4 key购买 nike

我有一个有效的 node.js 应用程序,我想将其部署到 AWS EB。它是有效的,因为它正在我的本地主机上运行:

npm 启动

我上传的 zip 如下所示:

folder structure

我的 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?但从文件夹结构来看它就在那里......

请告诉我出了什么问题,它已经困扰我很长时间了!

最佳答案

从您发布的代码中,我看不到实际启动服务器时的部分,但从日志中我看到它从端口 8081 启动,这对我来说实际上很奇怪,我想在 EBS 中它必须有一个动态端口

当您选择端口时,您应该使用process.env.PORT || 8081 这将获取 EBS 设置的端口或使用 8081(例如在本地)。

亚马逊有一个很好的指南,应该解释如何在那里部署 Node 应用程序:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html

关于node.js - 无法将我自己的 Node js部署到aws elastic beanstalk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32449463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com