How to run multiple js servers

拜拜、爱过 提交于 2021-01-28 11:03:28

问题


I'm developing a discord bot for discord at the moment, and to run the bot, I have a few different files. I've run into some problems along the way that all ended up having the same fix, or at least the only fix that I could come up with. That fix would be, instead of running them in my main file (index.js), I could just run 3 separate batch files using nodemon (https://www.npmjs.com/package/nodemon). I currently have 3 batch files to do so. Those batch files look like this:

nodemon index.js
pause

This would be for the main file.

nodemon logs.js
pause

This would be for the file that logs messages.

nodemon welcome and goodbye.js
pause

And this would be for the welcome and goodbye logs.

The only issue is, is that it clutters up my desktop with 3 different prompts, making it confusing as to what does what. I was wondering if it'd be possible with nodemon (or any other npm like this) for me to run all 3 of those batch files in one single command prompt.

And of course I'm open to other npms, but if I'd have to use another one, please include a way for it to automatically restart the server when I save the file in it as well (if possible). Since that's the main reason I like nodemon. I'd also like to be able to use a command prompt/batch file rather than the Visual Studio Code terminal.

If you're wondering what I mean when I say, "...automatically restart the server when I save the file," I mean this. (https://imgur.com/a/rw2Qagp if it doesn't show, don't know how to format this stuff) Whenever I save my code in Visual Studio it will restart and load the new code.

I'm still a little new to coding and stuff like that, so if my terminology is off I do apologize. If I did use a term incorrectly and you are confused/have a question about it, leave a comment and I will do my best too explain what I mean.

Thank you for taking time to read this.


回答1:


You can use concurrently or parallel in order to make it cross platform

example with concurrently:

package.json
{
  "scripts": {
    "serve": "nodemon index.js",
    "logs": "nodemon logs.js",
    "start": "concurrently \"npm:serve\" \"npm:logs\""
  },
  "devDependencies": {
    "concurrently": "^5.0.0",
  }
}

Edit: you may want to take a look at the VSCode Compound tasks

Here's an example setup




回答2:


Welcome :-)

I think you mean bash file (.sh) instead of batch file. So, in the terminal, you can && commands to chain them together.

For example, you could chain them together like this:

nodemon ./appOne/index.js && nodemon ./appTwo/index.js && nodemon welcome and goodbye.js

and they will all execute at one time. Additionally, you could make a bash file somewhere on your computer that did exactly what I typed above :-)

Now if you wanted them to go to the background and you wanted to forget about them, you could put them in the background with one ampersand... like so:

nodemon index.js &

and it would disappear and you'd have your console back. It would also print out a process ID (PID) of what that node process is under, so you can find it and kill it later (via kill -9 processid)



来源:https://stackoverflow.com/questions/58898386/how-to-run-multiple-js-servers

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