简体   繁体   English

如何设置 nodejs Web 服务器以异步运行?

[英]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.

相关问题 如何在Node.js中设置Web缓存代理服务器以减少Internet使用 - How can I set up a web cache-proxy server in nodejs to reduce internet usage 如何在Ubuntu中设置Node.js应用服务器 - How to set up a nodejs application server in ubuntu 您可以在Debian服务器上以Johnny-Five在后台运行nodejs吗? - You can run nodejs with Johnny-Five in the background on a Debian server? 在前端按下按钮后,如何在 web 服务器(nodeJS)中运行 python 脚本? - How can I run a python script in a web server (nodeJS) after pressing a button on frontend? 专用的Web服务器,nodeJS无法运行应用程序 - Dedicated web server, nodeJS can't run app 如何在 NATIVE nodeJS 中设置 SMTP 服务器 - 没有任何依赖关系 - How to set up SMTP server in NATIVE nodeJS — no dependencies whatsoever 如何与nodejs异步思考? - How to think asynchronously with nodejs? 如何在运行NODEJS服务器的AMAZON(AWS)上设置Web服务? - How to set a web service on AMAZON (AWS) which is running a NODEJS server? 如何在不重新启动服务器的情况下添加到Nodejs的路由? - How can you add route to Nodejs without restarting the server? nodejs - 在离线服务器上安装 reactjs 应用程序的生产版本,然后设置为作为服务运行 - nodejs - install production build of reactjs app on offline server, then set up to run as a service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM