使用pm2在AWS Ubuntu上运行nodejs app时出错

时间:2016-11-25 04:28:18

标签: node.js linux amazon-web-services ubuntu express

使用pm2在AWS ubuntu上使用express应用程序启动nodejs时,日志显示以下错误。 enter image description here

以下是我的app.js文件

// Packages
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 app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
//app.locals.basedir = path.join(__dirname, 'views');
global.__base = __dirname + '/';

var dbhelper = require('./routes/dbhelper.js');
var common = require('./routes/common.js');
var ErrorHandler = require('./routes/errorhandler.js');
var authenticationRepository = require(__base + 'repository/authentication.js');

// set up mustbe config
var mustBe = require("mustbe");
var mustBeConfig = require("./mustBeConfig");
mustBe.configure(mustBeConfig);

// Routes
var routes = require('./routes/index');

// Make database connection available to router
app.use(function (req, res, next) {
    common.ActionOutput.Message = '';
    common.ActionOutput.Results = [];

    req.ActionOutput = common.ActionOutput;
    req.ActionStatus = common.ActionStatus;
    req.ApplicationCookies = common.ApplicationCookies;
    req.Security = common.Security;
    req.Constants = common.Constants;
    console.log(req.url);
    // Temp Keys
    var key = common.Encrption('firstword|secondword');
    var dkey = common.Decryption('3TYkoj9CgZZU+IdD+PAqC3U9xlyJuG40g2fXAJn3GB8=');

    var key = req.get('Key');
    var msg = null;
    if (key == undefined || key == null || key == '')
        msg = 'header key not found.';
    else {
        try {
            var keys = common.Decryption(key).split('|');
            if (keys.length < 2 || keys.length > 3)
                msg = 'header key is incorrect.';
            else
                next();
        } catch (err) {
            msg = 'Unable to decrypt header value.';
        }
    }
    if (msg != null) {
        common.ActionOutput.Message = msg;
        common.ActionOutput.Status = common.ActionStatus.Error;
        return res.send(JSON.stringify(common.ActionOutput));
    }
});

app.use('/', routes);

app.use(function (err, req, res, next) {
    if (!err)
        return next();
    ErrorHandler.SaveErrorLog(req, res, null, err.message, err.stack, req.connection.remoteAddress, err.path);
});



// catch 404 and forward to error handler
app.use(function (req, res, next) {
    console.log('Exception: Not Found ' + req.url);
    var APIKey = req.get('Key');
    var UserKey = req.get('UserKey');
    var DeviceToken = req.get('DeviceToken');

    ErrorHandler.SaveErrorLog(req, res, UserKey, 'Resource not found!!!', '', req.connection.remoteAddress, req.url);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('shared/error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    var APIKey = req.get('Key');
    var UserKey = req.get('UserKey');
    var DeviceToken = req.get('DeviceToken');

    ErrorHandler.SaveErrorLog(req, res, UserKey, 'Resource not found!!!', '', req.connection.remoteAddress, req.url);
});

process.on('uncaughtException', function (err, abc) {
    console.log('uncaughtException: ' + err);
    ErrorHandler.SaveErrorLog(null, null, null, 'UnCaught exception: ' + err.message, err.stack, 'IP', err.address);
});


module.exports = app;

app.listen(3000, function () {
    console.log('Server starts on port: ' + 3000);
});
netstat在下面说,从外面访问它似乎是一个问题。应用程序在更改端口后运行但无法访问。 enter image description here 请建议。

0 个答案:

没有答案