Reputation: 799
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?
Upvotes: 20
Views: 15118
Reputation: 11
I created a nodejs loader for this situation: https://github.com/braidnetworks/dynohot
In this case you would modify the program as follows:
import http from 'node:http';
const server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('hello node');
}).listen(8080);
import.meta.hot?.accept();
import.meta.hot?.dispose(() => server.close());
And you would run the application via node --loader dynohot http_test.js
. Be sure to add "type": "module"
to your package.json file first. After that if you modify the string to 'hello node 2' and refresh in your browser you'll see the new text.
The advantage over nodemon is that dynohot can reload only the parts of the application which have changed without restarting the process. For larger applications the improvement in reactivity is very noticeable.
Upvotes: 0
Reputation: 1487
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 :-
https://github.com/remy/nodemon
Upvotes: 7
Reputation: 1087
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.
Upvotes: 0
Reputation: 19113
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
Upvotes: 24