How to keep MEAN (stack) running? - node.js

I'm using grunt to run the MEAN project on Ubuntu, but when I close the putty (I use putty to connect Ubuntu server from my PC), it would close the program too.
My question is how can I keep MEAN running?
Update: nohub grunt & stops after I close putty

There are various node based process managers which can serve your task. My favorite is pm2 (http://pm2.keymetrics.io/)
Package managers allow your program to keep running even in case of hiccups. They can watch your project directories for any changes that you might push to them and restart servers based on those changes.
Other favorite is forever (https://www.npmjs.com/package/forever).

you need to run the command in background and I would also recommend to use nohup so:
nohup grunt &
should do the trick.
https://en.wikipedia.org/wiki/Nohup

NODE_ENV=staging nohup node appStag.js &
You can use the above command to run node server
and you can get the above environment using process.env.NODE_ENV

I found a npm package called forever is a good solution, I use forever to run the program right now; and it works perfect with putty.

Related

Starting node js application using grunt on machine start up

I am new to both linux and Node js basically we have developed Node js application in Windows and I need to deploy it on Debian 8 Jessie and I am able to deploy it on linux and for this I need to install npm, node js, grunt cli etc.
And to run my application I just need to type grunt using terminal and application starts.
But the problem I need to start server every time after reboot of system by typing grunt in terminal.
So need solution how can I start my application/server on machine start.
Also let me know how this stuff works!!.
Thanks
as always there is more than one way
rc.local
the prefered way. rc.local will be executed on system startup.
to edit the file use your favourite text-editor (e.g. nano) nano /etc/rc.local and add your script before the last line containing exit 0
/usr/bin/myscript -arg1 -arg2
exit 0
cronjob
if there is also the need for recurring tasks (e.g a daily backup), cronjob could be a good choice to keep things together.
Within your terminal type sudo crontab -e to edit your cronjobs.
there add your command with the #reboot time argument.
#reboot /usr/bin/myscript -arg1 -arg2

How to run a node.js file as a background process on a Windows server?

I was creating a node.js project and uploaded it to my Windows server to provide an API service for mobile application.
When I open command prompt and type
node app.js
It runs correctly, but when I close the command prompt my node.js server stopped running.
How to make it still running when I close the commend prompt?
For example on Ubuntu I use the command
nohup
How can I do this on Windows?
You can make the process run in background using pm2
pm2 start app.js --watch
This will start the process and will also look for changes in the file.
More about watch flag
Nodemon #ftw. On Windows, Forever doesn't really watch files so much as casually observe them, while pm2 restarts the server every time you load a page.
Each of these tools works similarly, and each installs just as in the accepted answer. E.g.:
npm install nodemon -g
This installs nodemon globally, and to use you can simply navigate to your project folder and enter:
nodemon
(Assuming your project has an app.js file). You can add the -w switch, just as in Forever and pm2, however if you're just wanting to watch all files, you can omit that. To run nodemon in the background with Forever, you would run this:
forever nodemon --exitcrash
Nodemon is good for development, especially on Windows, while Forever and pm2 are more for production, on Linux.
Here is a simpler answer that cuts right to the chase without any added libraries or overhead like in the other two answers described above. To run your Node.js application as a windowless startup program in the background (this would be analogous to "nohup" in Linux), modify this template to suit and copy it into a .VBS script file. Then copy that file to your Start Menu startup folder (for all users, that would be C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup) and it will automatically run. The techniques you are using here in Visual Basic are (1) preparing to run the Node.js application by first changing the working directory of the shell object and (2) informing the shell to run the Node.js application in a hidden window by adding a “, 0” immediately after the run function:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\path-to-your-node-js-app\"
objShell.Run("""node"" your-app.js"), 0
Set objShell = Nothing
References:
https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
https://devblogs.microsoft.com/scripting/how-can-i-change-the-working-folder-of-a-script/
No, you can't.
Even if you make a GUI program you'll need to run it via console.
And as soon as you close the command prompt. Your service would be stopped/ terminated that moment only. Because node creates a server itself while running : http.createServer().listen(port) or app.listen(port). So this this makes it independent in nature.
So, as soon as you close the command prompt on which server was running all the services would stop at that moment.

Node.js - How do I detect if a node script is running?

I want to write a node script that detects if another node script is running.
My question is (all running on node in Windows 7), within a node script, how do I detect if another node script is running?
I'm new to node and I couldn't find an answer that matched my question exactly.
I'd be tempted to use nodemon...
Once installed (with npm install -g nodemon), instead of starting your server with say node app.js - start it with nodemon app.js.
Nodemon will listen for any changes to your files, and restart your server automatically for you. Perfect for development!
If this, for some reason, is not an option though, you could use something like the taskkill command to kill all the node processes with something like taskkill /F /IM node.exe
I'm not a Windows Guru though, so there's will likely be a better way of doing this with something like a batch script or windows service (rather than just killing ALL node apps)
Perhaps someone on StackOverflow with more Windows experience can help out here.
Good luck!

How can I run a node.js server

I'm new to node and have many things unclear.
Like, for php, I just need a index.php file on server's root dir and it can work by itself.
However, for a node.js file, we need to "node" command it in terminal right?
So what if we close that terminal? How can I keep it running to accept my requests?
You are correct in saying that the 'node' command will start a node process with whatever script you supply to it.
As far as keeping it running, there are several ways to do it. There are plenty of CLI libraries that will help you. For example, this one is called Forever
If you are using linux, you can simply run the node process as a background task:
node server.js &
To run node without terminal, you might want to check out one of these modules depending on your platform:
node-mac
node-windows
node-linux

how to run node js remotely

I am running node using putty. but it doesn't work when putty session expire. How can I start node js permanently, should not be terminated if putty session end.
I just solved that issue yesterday using Forever > http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever
It's awesome.
For Installing: npm install Forver
For running: forever start yourServer.js
For checking if its running: forever list
cool eh?
Generally you use a deamon to keep it running. A proper answer depends on what type of OS your remote machine is running (windows ?).
It's best to run node.js on *unix.
Among what Raynos and nEEbz are suggesting you can also try to use GNU Screen. This is very handy especially if you are using putty to connect to remote server. Check out this screen tutorial for more information.
Here is a quick and gentle introduction to "screen" .
In ubuntu, if you need to install it, use: apt-get install screen
First use:
$ screen
bunch of stuff prints out, then another shell prompt
$ node ./myapp.js
now your node app is running
You want to edit some other code?
control-a c
the window clears, and you now have another shell prompt. node is still running....
$ edit public/somewebfile.html
save it, still in editor
go back to node
control-a control-a
screen switches back to the screen running node
need to leave the office for a few minutes
control-a control-d
(screen detaches from your location... processes remain attached)
$ logout
screen disconnects, but nodejs and the editor are still running...
back at home... want to connect to work
> ssh work.some.where
> screen -D -R
screen reconnecting....
now you see the nodejs shell screen again, or control-a control-a to switch back to that editor
control-a ? shows available commands, or read the nice man page: man screen
You can also run nohup:
nohup node app.js
If you get an error when you do npm install Forever, do npm install forever -g
I got that problem.
https://www.npmjs.com/package/forever
Use PM2
just install pm2 on the server and run app like this : pm2 start app.js
you can also monitor your application from the web panel
PM2 (proccess manager)

Resources