Error executing hello world in node

0

Good a query happens that I installed node with installer in my pc, but when I execute it does not respond any idea? here my script to explain me better

var http = require('http');

var manejador = function(solicitud, respuesta){
    console.log("Hola mundo");
    respuesta.end();
}

var servidor = http.createServer(manejador);
servidor.listen(8080);

    
asked by Eduardo Palomino 05.09.2017 в 19:04
source

1 answer

2

You are raising an http server with node.js therefore the "hello world" message will appear when you access that server. As a good practice, I recommend adding the following to your code:

var http = require('http');

var manejador = function(solicitud, respuesta){
    console.log("Hola mundo");
    respuesta.end();
}

var servidor = http.createServer(manejador);
servidor.listen(8080, function(){
    console.log("Servidor http corriendo en http://localhost:8080");
});

To test your code, follow these steps:

1.-From cmd run the node server.js command again

2.-In the address bar of a browser (chrome, firefox, opera, etc) enter link

3.-In cmd the message hello world will appear

4.-To stop the server press ctrl + c

Regarding the node help command (if you want help for node commands) type node -h

    
answered by 05.09.2017 в 19:35