codegurukool.com · Free coding tutorials & online compiler

Home › Node.js Tutorial › Node.js HTTP server

Node.js HTTP server

Create a web server.

The http module builds servers. http.createServer takes a function that receives a request and a response. This example calls itself, then stops.

Example code

const http = require("http");
const s = http.createServer(function (req, res) {
  res.end("Hello client");
});
s.listen(0, function () {
  http.get({ port: s.address().port }, function (r) {
    let d = "";
    r.on("data", function (c) { d += c; });
    r.on("end", function () { console.log(d); s.close(); });
  });
});

Run this code in the online editor

← Node.js async and awaitNode.js URL and query strings →