我正在尝试在我的快速服务器中定义一个端点,无论何时调用此端点,服务器都会在运行时自动重启。 例如,使用express我的服务器看起来像这样......
var express = require('express')
var app = express();
app.post('/restart', (req,res)=>{
//restart or create a new instance of the server
// then reply
res.json({
'message': 'server restarted successfully'
})
})
// =======================
// start the server ======
// =======================
var port = process.env.PORT || 8000;
app.listen(port);
console.log('server running at http://localhost:' + port);
注意:虽然我使用的是expressJS,但我对HAPI等其他解决方案持开放态度。
提前致谢
答案 0 :(得分:0)
我知道如何重新启动节点实例的唯一方法是在CLI级别通过npm forever或pm2,但这是针对部署级别xP。
答案 1 :(得分:0)
您需要在系统上全局安装npm模块forever
,并将Shelljs
作为依赖项。最初以forever start {Path to server.js}
启动服务器。然后就可以了
var express = require('express')
var shell = require('shelljs')
var app = express();
app.post('/restart', (req,res)=>{
//restart or create a new instance of the server
shell.exec('forever restart {Path to server.js}');
// then reply
res.json({
'message': 'server restarted successfully'
})
})
// =======================
// start the server ======
// =======================
var port = process.env.PORT || 8000;
app.listen(port);
console.log('server running at http://localhost:' + port);
另请注意,由于服务器已重新启动,您将无法获得响应。你只会拒绝连接。
答案 2 :(得分:0)
您可以使用PM2启动,使用简单命令停止服务器。
在生产模式下启动应用程序非常简单:
pm2 start app.js
停止所有应用
pm2 stop all
重启所有应用
pm2 restart all
我希望这对你有用。
HTH谢谢!
答案 3 :(得分:0)