Is it possible to restart the Nodejs server when a modification occurs automatically?

Asked

Viewed 928 times

4

Every time I try to make some modification in my file app.js I have to close and start the server for the modifications to take effect, would there be any way to automatically restart after a change? as in php.

Example:

var http = require('http');

var server = http.createServer(function(request, response){
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write("<h1>Hello World!</h1>"); // <-- se mudar o texto no F5 não atualiza.
    response.end();
});

var servidorLigou = function () {
    console.log('Servidor Hello World rodando!');
};

server.listen(3000,servidorLigou);

1 answer

3


You can use a supervisor for this function. An alternative to this role is the nodemon.

To install it is very simple, as the site shows:

$ npm install nodemon -g
$ nodemon app.js

This question has several options of Supervisors and how to use, if you are interested.

  • 1

    Oops, that was it. so I should use the forever in production and the developing nodemon? do not know why it does not come by default o.O

  • 1

    @Gabrielrodrigues does not come by default because there are more ways to do and are environment-specific and independent solutions (server operating system) in which Node run.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.