Minimal HTTP Server in Node.js without Framework

Update: Check out my NodeJS Server without Express article if you are interested in serving static as well as dynamic content.

Following is the code for the HTTP server written in Node.js without using any framework.

Place this code in

minimal-http-server/index.js

then import the server in your code

const server = require('./minimal-http-server');

Start the server as follows:

server.init();

The server will start on port 3000. Alternatively, you can specify the port as the first argument and hostname as the second argument to the init() method as shown below:

server.init(4000, 'myserver');

After writing this code, I found a similar code on MDN also.

View Source