在生产环境中十秒钟后,Node JS应用程序关闭,但在本地环境中运行良好

时间:2019-06-21 10:41:36

标签: javascript mysql node.js

我有一个Node JS应用程序,它充当React Web应用程序的后端。该应用程序在启动时将服务于React应用程序并处理所有API调用。我连接到MySQL数据库以获取数据。该应用程序可在本地环境上正常运行,但是在Ubuntu服务器上(AWS)部署时崩溃。在服务器上,该应用正常启动,但几秒钟后崩溃。 这是启动时抛出的错误

Error connecting: Error: connect ETIMEDOUT
    at PoolConnection.Connection._handleConnectTimeout (/home/ubuntu/localSystem/node_modules/mysql/lib/Connection.js:412:13)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at Socket._onTimeout (net.js:422:8)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
    --------------------
    at Protocol._enqueue (/home/ubuntu/localSystem/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/ubuntu/localSystem/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/ubuntu/localSystem/node_modules/mysql/lib/Connection.js:119:18)
    at Pool.getConnection (/home/ubuntu/localSystem/node_modules/mysql/lib/Pool.js:48:16)
    at Object.<anonymous> (/home/ubuntu/localSystem/model/db.js:29:12)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)

数据库连接文件(db.js):

var connection = mysql.createPool({
    connectionLimit: 100,
    host     : 'xxxx',
    port : xxxx,
    user     : 'username',
    password : 'password',
    database : 'myDB'
});
connection.getConnection(function(err) {
    if (err) {
        console.log('Error connecting: ' + err.stack)
        return;
    }
    console.log('Connected as id ' + connection.threadId)
});

module.exports = connection;

Server.js

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');

app = express();
var appRoutes = require('./routes/appRoutes');
app.use(express.static(path.join(__dirname, './client/build')));
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/api', appRoutes);


module.exports = app;

index.js

var app = require('./server');
//const { connection } = require('./database');

var port = 5001;

app.set('port', port);

app.listen(app.get('port'), () => {
    console.log(`server on port ${app.get('port')}`);
});

AppModel.js

var sql = require('./db.js');

//Task object constructor
var Task = function(task){
    this.task = task.task;
    this.status = task.status;
    this.created_at = new Date();
};

Task.getAllCustomerStatusRecords = function getAllCustomerStatusRecords(result) {
    sql.query("Select a, b, c, call_provider_id from myTable order by id desc limit 20", function (err, res) {
            if(err) {
                console.log("error: ", err);
                result(null, err);
            }
            else {
              console.log('Customer status records : ', res);  
              result(null, res);
            }
        });
      sql.releaseConnection() 
};

我为另一个Node应用设置了类似的设置,但是没有mysql,并且运行没有问题。我不确定为什么在这种情况下它会崩溃。

编辑: API请求的确进行了数据库调用。但是,只有当用户明确选择一些选项然后在网页上进行访存调用时,才可以。不过最初,没有对Db或外部服务的向外API调用。

0 个答案:

没有答案