how to reload the nodejs app after making changes to the file? - javascript

i have a simple js file with http module to test Hello node for nodejs....
below is the http_test.js file
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200);
res.end('hello node');
}).listen(8080);
node http_test.js prints fine on the browser...now if i change my response line to say res.end('changed hello node to hello stoner');, i still get previous hello node on my page....
to get the changed line i got to end the current instance of node and then again run
node http_test.js
if i make any changes to js file should i restart over?
wouldnt just hitting refresh on my browser do it?

You need to stop and re run the server to see your latest update. To automate this, you can use nodemon, do npm i nodemon -g
And run nodemon http_test.js
Now, for every change you make tohttp_test.js the server will be restarted automatically

1) Install nodemon. To install, from your terminal run:
npm install -g nodemon
2) Now, go to terminal, where you have the program. And, run
nodemon http_test.js
Now, everytime when you make changes to your app, just save your changes and it will get reflected.
Details :-
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.
Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes.
Please refer :-
http://nodemon.io/
https://github.com/remy/nodemon

You have to restart your server, because the file had load to the memory .
Or you can use nodemon who auto restart your server when you change file.

Related

Is there a Git Bash command to deactivate all my active localhosts at once?

When creating a react app, I run these commands in my terminal:
npx create-react-app app-name
npm install
npm start
npm start allows me to view my project locally, but I accidently closed my terminal before running ctrl + c to end it. The browswer tab containing localhost 3000 (the npm start default host) has been closed for a while now, but my project can still be viewed there everytime I follow that link. Now everytime I run npm start it tells me that localhost 3000 is taken and I have to input another command and run it on localhost 3001. I've made this mistake a few times. I don't mind doing this, but it got me wondering if there is a global version of ctrl + c that will close all of these at once.
Firstly, I think you've had a misconceptions here: closing the browser tab will never end the server that you've started (i.e. the server npm start created).
For your question I think you can use the approach of killing a process based on the port they're occupying in this question:
Find (and kill) process locking port 3000 on Mac

How to run node.js application from script?

I am trying to run my node.js application from a script I have written:
echo "Starting node application"
sudo node /home/pi/PPBot/bot.js
exit 0
I run the script like this: sudo /etc/init.d/botscript
The output when running the script is:
Start node application
sudo: node: command not found
I have also tried replacing node by /home/pi/.nvm/versions/node/v.8.11.3/bin/node but this resulted in the same output.
I have already installed NodeJS through NVM. Simply using the command node bot.js works from the command line. However as can be seen above it does not work through the script.
if you want to run a simple node js file then use the command
-> node filename
ex.: node server.js
if you want to run a node js file with nodemon then use the command
-> nodemon filename
ex.: nodemon server.js

Http-server running in background with nohup doesn't remain live

I have an ubuntu server. On which I have transferred some files which have some js and html code.
I used http-server from node.js to start a web server so that I can display the html page on the server.
I used nohup so that it can remain running even if I disconnect or close my system.
Here is the command I used:
nohup http-server -p 8000 -a 10.4.145.182 &
Now this helps me visualize the files on 10.4.145.182:8000 but I am noticing after sometime the server goes down and one can't access the html page on that ip in their browser.
I thought nohup helps run things in background even if one closes their system or logs out of server.
How do I make this web server running always then and accessible to url to everyone
Thanks
EDIT:
As per the suggestion below of using pm2, I installed latest version of node and then started service with pm2.
It says service started for http-server but when I go the to ip with port 8000 on browser it doesn't open up.
Here is the command I ran in my directory which has the html and d3 files.
$ pm2 start $(which http-server) -p 8000
And here is the output in shell of the pm2
Try this:
http-server & exit
You can also specify a port number:
http-server -p 8082 & exit
Reference here
I suggest you use pm2 . Long story short , check the official link https://www.npmjs.com/package/pm2
Steps :
Install :
npm install pm2 -g
Run your app :
pm2 start app.js
In your case use :
pm2 start /usr/local/bin/http-server -- -p 8080
for reboot to work run :
pm2 startup systemd
take the last line , change the user and home path and run the modified line as SUDO !!! you need sudo access for this !!!

Bot shuts down when putty window is closed

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:
sudo apt-get install -y nodejs
Then I used cd to select the directory, and started my bot using node index.js
The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.
I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.
To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.
The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like
tmux list-sessions
tmux attach-session -t 0
The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:
PM2: http://pm2.keymetrics.io/
or,
Forever: https://github.com/foreverjs/forever#readme
Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this:
https://stackoverflow.com/a/29042953/7739392
The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:
node index.js &
A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.
I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.
You can easily install ForeverJS by typing the following in your terminal:
$ npm install forever -g
You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.
Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.
$ forever start index.js
Now when you exit the terminal your NodeJS application will remain as a running process.

Express not listening on localhost:3000

So, we've built a basic express node website
Trying to run the app with DEBUG=express_example:* npm start
With node DEBUG=express_example:* npm start
Also, tried inside node runtime:
http://localhost:3000/ is not connecting
Where are we wrong?
You need to create a variable called DEBUG with set command.
There is not command like DEBUG, it is a name of variable, so please try to run your server with set (to create variable):
set DEBUG=express_example:* & npm start
Try
DEBUG='express_example:*' npm start
Your environment variable was not getting set properly. Note that you can have many different environment variables this way
TEST=foo DEBUG='bar' npm start

Categories