Basic web application in Node.js to return text

Here we write a basic web application in Node.js that serves static text to the client’s HTTP requests.

var http = require('http');
var server = http.createServer();
server.on('request', function(request, response) {
    response.end('Hello World');
console.log('Server started. Hit http://localhost:8080');
  • Added an event listener on the server variable that listens to the request events.
  • The event listener takes a callback function with two arguments, request and response.
  • If there is a request, node.js will raise a request event adding the respective request listener to it
  • The request listener nothing but the callback function, will gets executed to return “Hello World” to browser.

Leave a Comment