user938363
user938363

Reputation: 10360

Nodejs server failed to response after moving to cloud

I just moved the nodejs 10.16.3 server to cloud to test with React native 0.61 app and postgres 11. The android emulator used to work with the Nodejs sever and postgres 11 server running on the same PC. But the emulator throws Network failed error after moving to the cloud:

10-03 12:07:48.495 17334 17384 I ReactNativeJS: 'Signup post URL : ', 'http://myip:3000/api/users/signup'
10-03 12:07:48.513 17334 17384 I ReactNativeJS: 'obj : ', '{"_device_id":"ce0e244d684db","cell":"1234563200","cell_country_code":"1","name":"jccc","corp_name":"i am here"}'
10-03 12:09:03.370 17334 17384 I ReactNativeJS: 'error signing up: ', { [TypeError: Network request failed]  //<<<==== ERROR on RN app
10-03 12:09:03.370 17334 17384 I ReactNativeJS:   line: 26749,
10-03 12:09:03.370 17334 17384 I ReactNativeJS:   column: 31,
10-03 12:09:03.370 17334 17384 I ReactNativeJS:   sourceURL: 'http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false' }

The nodejs server is started with pm2 start index.js successfully and pm2 logs brings up the log below:

ubuntu@ip-12-30-0-93:~$ pm2 logs
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/home/ubuntu/.pm2/pm2.log last 15 lines:
PM2        | 2019-10-01T04:52:29: PM2 log: Time                 : Tue Oct 01 2019 04:52:29 GMT+0000 (Coordinated Universal Time)
PM2        | 2019-10-01T04:52:29: PM2 log: PM2 version          : 3.5.1
PM2        | 2019-10-01T04:52:29: PM2 log: Node.js version      : 10.16.3
PM2        | 2019-10-01T04:52:29: PM2 log: Current arch         : x64
PM2        | 2019-10-01T04:52:29: PM2 log: PM2 home             : /home/ubuntu/.pm2
PM2        | 2019-10-01T04:52:29: PM2 log: PM2 PID file         : /home/ubuntu/.pm2/pm2.pid
PM2        | 2019-10-01T04:52:29: PM2 log: RPC socket file      : /home/ubuntu/.pm2/rpc.sock
PM2        | 2019-10-01T04:52:29: PM2 log: BUS socket file      : /home/ubuntu/.pm2/pub.sock
PM2        | 2019-10-01T04:52:29: PM2 log: Application log path : /home/ubuntu/.pm2/logs
PM2        | 2019-10-01T04:52:29: PM2 log: Process dump file    : /home/ubuntu/.pm2/dump.pm2
PM2        | 2019-10-01T04:52:29: PM2 log: Concurrent actions   : 2
PM2        | 2019-10-01T04:52:29: PM2 log: SIGTERM timeout      : 1600
PM2        | 2019-10-01T04:52:29: PM2 log: ===============================================================================
PM2        | 2019-10-01T04:53:40: PM2 log: App [index:0] starting in -fork mode-
PM2        | 2019-10-01T04:53:40: PM2 log: App [index:0] online

/home/ubuntu/.pm2/logs/index-out.log last 15 lines:
0|index    | undefined
0|index    | env var: undefined
0|index    | Listening on port undefined...

/home/ubuntu/.pm2/logs/index-error.log last 15 lines:
0|index    |    { Error: connect ECONNREFUSED 127.0.0.1:5433
0|index    |        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
0|index    |      errno: 'ECONNREFUSED',
0|index    |      code: 'ECONNREFUSED',
0|index    |      syscall: 'connect',
0|index    |      address: '127.0.0.1',
0|index    |      port: 5433 },
0|index    |   original:
0|index    |    { Error: connect ECONNREFUSED 127.0.0.1:5433
0|index    |        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
0|index    |      errno: 'ECONNREFUSED',
0|index    |      code: 'ECONNREFUSED',
0|index    |      syscall: 'connect',
0|index    |      address: '127.0.0.1',
0|index    |      port: 5433 } }

The log message seems vague to me and I have no clue where is exactly the problem. What is a better way to console log and debug Nodejs server in cloud (like with the console.log output)?

Here is the db.js in Nodejs server app:

const express = require("express");
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const Sql = require("sequelize");
const db = new Sql('emps', 'postgres', `${process.env.DB_PASSWORD}`, {
    host: 'localhost',
    dialect: 'postgres',
    port:5433,
    //operatorsAliases: false
} );

db
    .authenticate()
    .then(() => {
        console.log('DB connection has been established successfully.');
        // The event will be called when a client is connected.

    })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });

module.exports = db;

Upvotes: 0

Views: 144

Answers (1)

Allen Haley
Allen Haley

Reputation: 657

Your port is undefined. It seems that your .env file has some problems.

...
0|index    | env var: undefined
0|index    | Listening on port undefined...

Upvotes: 1

Related Questions