Your Friend
Your Friend

Reputation: 1149

Node js refresh browser

I am pretty new to node js...so this question is very basic question.... lets say this is my simple node js code...

var http = require('http');
    http.createServer(function(req,res){
        res.writeHead(200,{'content-type':'text/html'});
        res.write('<doctype html><html><body>testing node ...</body></html>');      
        res.end();
    }).listen(8888,'127.0.0.1');


    console.log('Server running at http://127.0.0.1:8888');

yes it works fine... But let say we make a very small change...if we refresh the browser it does not apply the changes made..

below is my procedure...

1)stop process running on port 8888 2) start node again... 3)refresh the browser...

is this the correct way? everytime we have to do this to apply changes?

Upvotes: 2

Views: 2980

Answers (4)

Vaghani Janak
Vaghani Janak

Reputation: 611

You need to Install node nodemon tool

npm install nodemon --save

and after open cmd and set your project path and run

nodemon server file

ex. nodemon server.js

and after you any changes server auto restart....

Upvotes: 1

himanshu yadav
himanshu yadav

Reputation: 1908

use code like this:

   app.get('/', function (req,res) {
       res.sendFile(__dirname + '/public/login.html');
   });

Upvotes: 0

tim-montague
tim-montague

Reputation: 17372

Use Supervisor (npm install Supervisor) to automatically restart your server when you make server-side development changes, and use Reload (npm install Reload) to refresh your browser when you make client-side development changes.

Upvotes: 1

Paramore
Paramore

Reputation: 1313

Yes, in your case you should restart application to apply changes. But you can put your HTML code in some html file and send that file to client, then you will not need to restart application if you make changes in html file. Also consider using Express module to serve static files.

Upvotes: 2

Related Questions