How to avoid logging errors from restarting NodeJS server

为君一笑 提交于 2021-01-07 01:39:47

问题


I use NodeJS as a web server on an AWS virtual machine listening on 8443 and leave it running with:

kill $(lsof -i :8443 | grep .node.bin | cut -d " " -f 2); cd $HOME/server; NODE_ENV=production npm start  >> stdout.txt 2>> stderr.txt &

I monitor the error log. Every time I push changes and restart the server, the error log gets several lines, such as:

/opt/bitnami/nodejs/bin/.node.bin[31215]: ../src/node.cc:663:void node::ResetStdio(): Assertion `(0) == (err)' failed.
 1: 0x9ef190 node::Abort() [/opt/bitnami/nodejs/bin/.node.bin]
 2: 0x9ef217  [/opt/bitnami/nodejs/bin/.node.bin]
 3: 0x9bd657 node::ResetStdio() [/opt/bitnami/nodejs/bin/.node.bin]
 4: 0x9bd6c0 node::SignalExit(int) [/opt/bitnami/nodejs/bin/.node.bin]
 5: 0x7f7e4922a390  [/lib/x86_64-linux-gnu/libpthread.so.0]
 6: 0x7f7e48f56ad3 epoll_wait [/lib/x86_64-linux-gnu/libc.so.6]
 7: 0x13200b0  [/opt/bitnami/nodejs/bin/.node.bin]
 8: 0x130e26b uv_run [/opt/bitnami/nodejs/bin/.node.bin]
 9: 0xa31ec3 node::NodeMainInstance::Run() [/opt/bitnami/nodejs/bin/.node.bin]
10: 0x9c1cc8 node::Start(int, char**) [/opt/bitnami/nodejs/bin/.node.bin]
11: 0x7f7e48e6f840 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
12: 0x95c085  [/opt/bitnami/nodejs/bin/.node.bin]
Aborted (core dumped)
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! EmotionAthletes@0.0.1 start: `node app.js`
npm ERR! Exit status 134
npm ERR! 
npm ERR! Failed at the EmotionAthletes@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/bitnami/.npm/_logs/2020-10-25T12_30_23_826Z-debug.log
npm[31199]: ../src/node.cc:663:void node::ResetStdio(): Assertion `(0) == (err)' failed.
 1: 0x9ef190 node::Abort() [npm]
 2: 0x9ef217  [npm]
 3: 0x9bd657 node::ResetStdio() [npm]
 4: 0x7f7db55c4008  [/lib/x86_64-linux-gnu/libc.so.6]
 5: 0x7f7db55c4055  [/lib/x86_64-linux-gnu/libc.so.6]
 6: 0x994907  [npm]
 7: 0xbc9a29  [npm]
 8: 0xbcb817 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [npm]
 9: 0x13a72b9  [npm]

How can I leave it running in the background and avoid logging restart errors?


回答1:


I got the answer from Difference in NodeJS termination errors :

const express = require('express');
const http = require('http');

const app = express();
const httpServer = http.createServer(app);
let server = httpServer.listen(8080);

process.on("SIGINT", () => {

  // Close main server and HTTP redirection server.
  server.close();
  mongoose.connection.close();
  console.log("SIGINT received, server stopped");
});

Then I can stop the server without errors with Control-C (^C) or restart it and leave it running in the background with:

kill -s SIGINT $(lsof -i :8080 | grep .node.bin | cut -d " " -f 2); cd $HOME/server; cp config.prod.js config.js; NODE_ENV=production npm start  >> stdout.md 2>> stderr.md &

In production, the server listens on 8443 for SSL and I have a small server that redirects everything on 8080 to 8443, so I need to stop both servers or else I cannot restart because one port is in use:

let server, httpServer;

const https = require('https');
// SSL configuration
const credentials = {key: sslPrivateKey, cert: sslCertificate};
const httpsServer = https.createServer(credentials, app);
  
server = httpsServer.listen(8443);

// Also create a small server to redirect HTTP to HTTPS.

const http = express();

// Set up a route to redirect http to https.
http.get('*', function(req, res) {  
  res.redirect('https://' + req.headers.host + req.url);
});

// Have it listen on 8080.
httpServer = http.listen(8080);

process.on("SIGINT", () => {
  // Close main server and HTTP redirection server.
  server.close();
  httpServer.close();
  mongoose.connection.close();
  console.log("SIGINT received, server stopped");
});

I use ports 8080 and 8443 for security because ports under 1000 (80 for HTTP and 443 for HTTPS) are reserved ports. See Node.js EACCES error when listening on most ports. So instead of giving the node server root privileges, I run these shell commands to redirect traffic to on those ports to other ports:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -t nat -D PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443


来源:https://stackoverflow.com/questions/64542392/how-to-avoid-logging-errors-from-restarting-nodejs-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!