Restart Node.js server programmatically

后端 未结 2 1301
生来不讨喜
生来不讨喜 2021-01-03 23:53

How to restart Node.js server from code? For example, if using expressjs framework,

app.get(\'/restart\', function (req, res, next) {
//Code to restart a se         


        
相关标签:
2条回答
  • 2021-01-04 00:11

    By combining npm restart and child_process.exec() it appears possible to restart the server programmatically. And since it allows for several scripts to be run for stopping, restarting, and starting this option would work even for multi-process servers or otherwise complex servers where simply calling process.exit() isn't viable.

    0 讨论(0)
  • 2021-01-04 00:21

    I use forever in order to start and monitoring application. So the restart function like this:

    app.get('/restart', function (req, res, next) {
      process.exit(1);
    });
    

    After the shutdown of server, forever will restart service.

    console:

    Express server listening on port 3000 in development mode
    error: Forever detected script exited with code: 1
    error: Forever restarting script for 2 time
    Express server listening on port 3000 in development mode
    
    0 讨论(0)
提交回复
热议问题