How run Express Js/MongoDB in command line and close the terminal - javascript

I wanna know how can I run an express project but hide or close the terminal.
if I close the terminal my server fall, but I wanna hide it or something I'm doing a project in a local pc and me don't wanna that the user watches or close that and the app fall.

Use a process manager like PM2 or forever.
Forever example command: forever start app.js

Try using nohup <command>
sudo nohup npm start
A long version answer is to use docker-compose with node js and run services which works in the background by running:
docker-compose up -d <service_name>

Related

Running the server in Local Machine without showing cmd the window

I' ve created my node.js app with express, now i want to upload it on my local server machine.
I've created a bat file (I will use task planner to run this) that basically move to folder app and launch "npm run start".
The question is: There's a more correct way to this? (no cloud) and is it possible to hide the cmd window?
Putting a node.js application to production can be done with pm2. Pm2 will spin up your application and keep it alive, without having any terminal/cmd open.
Simply install pm2 globally on your machine:
$ npm install pm2 -g
Then launch your application with pm2:
$ pm2 start app.js
There are some configurations you could do, so I suggest you look at their repo

Nodemon not working on new project with express

I have a new project folder configured and running with express.js and the server is running fine on port 3000.
I have installed nodemon globally using the sudo command so I expect I don't need to add this as a dependency or locally within the project.
When installing nodemon there are no errors, however when i fire the command nodemon server.js the command line essentially doesn't do anything and stops accepting commands.
I'm wondering if I'm missing something and hoping someone can point me in the right direction.
Thanks in advance for your time.
Use npm install -g nodemon this command to install nodemon globally and try to run nodemon [your node app]. For better understanding go through link.
Evening all, thanks for your contributions earlier, essentially things were mixed up with the course since the updates which was causing my issue.
Previously it looks like you would install express and run server.js through your designated port and then boot up nodemon. This no longer works and was causing the issue I was having.
Now you simply just boot up nodemon via the command nodemon server.js and it handles the rest. This wasn't really outlined in the documentation.
I was expecting to boot up localhost and then use nodemon to watch for changes. Looks like the new method is much more efficient
Thanks for the help

pm2-windows-startup stopped working after Windows reinstallation

I use PM2 module and pm2-windows-startup utility to automatically run Node.js when Windows startups. It was working perfectly during a few months, but recently once I reinstalled my Windows, the utility stopped working.
When I run Node by command promt with
pm2 start myapp.js --watch
I get the message
PM2 is being successfully deamonized
Then if I run the command
pm2-startup install
I get
Successfully added PM2 startup registry entry.
And then if I run
pm2 save
I get
PM2 is being successfully deamonized
Saving current process list...
Nothing to save !!!
But if I restart my computer, the utility doesn't start Node.js
How to resolve the problem?

What is difference between node and nodemon?

in my package.json I am using
"scripts": {
"start": "node app.js"
},
but if I use nodemon replace with node app.js like
"scripts": {
"start": "nodemon app.js"
},
then what will happen? Because when I got any error at server side, other API also close working. So I think it happen because I use node app.js if I use nodemon app.js than server will restart or not.
When you develop a node app and you make some changes, to see them in effect you have to restart the server.
When you launch your node.js application with Nodemon it will monitor for any changes and automatically restart the server, improving your productivity.
Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development. Install it using npm.
npm install -g nodemon
How to use nodemon?
nodemon "filename" ignore the quotation and place name of the server file.
Nodemon:
monitors for any changes in your Node.js application
automatically restarts the server,
saving time and tedious work.
it's one way to make your development efficient with Opn:
Opn is a dependency that opens web browser links, files, and executables. We will be using Opn to automatically open a web browser to our local host every time our server restarts.Install with npm
npm install opn.
How to use node?
node "filename" ignore the quotation and place the filename (ex app.js ,server.js)
node:
no automatic restart the server every time you do the tedious work
no monitors for any change
nodemon is like a live-server for your node application. any changes made in your node application will get reflected as server will restart again.
as stated here :
nodemon will watch the files in the directory in which nodemon was
started, and if any files change, nodemon will automatically restart
your node application.
nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
To use nodemon, replace the word node on the command line when executing your script.
In terminal,instead of typing node app.js,you can type: npm start
In package.json file,you can change it to:
"scripts": {
"start": "nodemon app.js"
},
In short,it is like a live server for node js, like we have in HTML & CSS.
When you are using node you have to restart on your own to see the changes you made But nodemon watches the particular path for any changes.If you make any changes in your file, nodemon will restart it for you.
when we install node, we will get automatically node and npm global variable.
for using nodemon you need to install it
npm install -g nodemon
we can access files with node as well but each time when we do changes we need to stop server and restart it.
node "filename" // provide filename
but if we accessing file with nodemon you no need to stop server and restart it only one line of command will save restart server time
nodemon "filename" // provide filename
this one line helps you saving lot of development time and test your sample javascript code
Nodemon stands for Node Monitor.
When you run a server using the command node index.js, after every change in your code you have to again run the node index.js command and reload the page to see the changes. Nodemon solves this problem for you. It auto-updates the server for you.

How can I start the node red with forever in my linux server?

I am installed node-red with npm and able to run the node red by just typing the 'node-red' in terminal that's it. But how can I run the node red in linux server with forever command ? I want node red running continuously.
You can start a script as a daemon. But first install forever globally: npm -g install forever (you'll need root privileges to do that). Then issue command forever start /path/to/node-red red.js.
First you should've noticed that when you installed nod-red, it says the path to your file, so for me it was
/usr/bin/node-red -> /usr/lib/node_modules/node-red/red.js
Then, eventually I run this command and it works smoothly.
forever --minUptime 1000 --spinSleepTime 1000 /usr/lib/node_modules/node-red/red.js

Categories

Resources