Hello World HTTP Server in Node

neotam Avatar

Hello World HTTP Server in Node

Tags :

Learning the node for backend web application enables makes you the full stack web developer. Using node for backend web application will leverage the strength of asynchronous and even driven nature of javascript

An example of hello world web application

const http = require('http');

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Hello, World! Welcome');
}

const server = http.createServer(requestListener);
server.listen(8090);

To run the above code, assuming it is saving into app.js

node app.js 

Running server can be verified using curl

curl localhost:8090 

Explanation of the code

http.createServer function take the function which takes request, and response as arguments. When request is received requestListener function will be called

To listen on port 8090, call server.listen(8090)

Leave a Reply

Your email address will not be published. Required fields are marked *