Hello World HTTP Server in Node

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)

Default image
neotam
Naveen T aka neotam. Programming language agnostic, Software architect, Python expert, Networking & DevOps engineer & consultant with 7+ years of experience in creating serious web applications, real time event-driven non blocking applications and database driven applications ranging from small scale to enterprise grade. website
Leave a Reply