[英]How can you set up a nodejs web server to run asynchronously?
Okay, so I'm in the process of creating a Minecraft panel.好的,所以我正在创建一个 Minecraft 面板。 I have a windows service, and that will call my express server.我有一个 Windows 服务,它将调用我的快速服务器。 The problem is, that when the computer tries to start the process, it goes on infinitely because the server will run for eternity.问题是,当计算机试图启动进程时,它会无限地运行,因为服务器将永远运行。 I need this to be changed so that when it runs it will start up the web server, make sure it's running, and then finish the task and forget the webserver exists, but leave it running.我需要对此进行更改,以便在它运行时启动 Web 服务器,确保它正在运行,然后完成任务并忘记 Web 服务器的存在,但让它继续运行。 How would this be possible?这怎么可能? Many thanks :)非常感谢 :)
Update: After a little bit of though I'm going to refine my question to this: How can I start a process and not wait for it to finish in C#/nodejs(either work)更新:经过一点点,虽然我将把我的问题细化为:如何启动一个进程而不是等待它在 C#/nodejs 中完成(要么工作)
Edit: Lol i am refreshing this page like every two microseconds.... Its 4 AM for me so yeah :D brain - broke编辑:大声笑,我每两微秒刷新一次这个页面......对我来说是凌晨 4 点,所以是的:D 大脑 - 坏了
Answer: So I figured out how to fix it.答:所以我想出了如何解决它。 I used pm2 which can start a process and not wait for it to finish.我使用 pm2 可以启动一个进程而不是等待它完成。 We have a start.js and a stop.js which can start it.我们有一个 start.js 和一个 stop.js 可以启动它。 If you want to build something like it here is the code I used.如果你想在这里构建类似的东西是我使用的代码。
//Start.js
const pm2 = require('pm2');
pm2.connect((err) => {
if (err) {
console.log(err);
}
});
pm2.start({
name: 'webserver',
script: "webserver.js",
}, (err) => {
if (err) {
console.log(err)
} else {
console.log('Webserver started');
pm2.disconnect();
process.exit(0)
}
})
//Stop.js
const pm2 = require('pm2');
//Stop the webserver
pm2.stop('webserver', (err) => {
if (err) {
console.log(err);
} else {
console.log('Webserver stopped');
pm2.disconnect();
process.exit(0)
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.