Creating a Server in Node.js

A guide to create a server in Node.js.

Edit index.js file with the code:

const http = require('http');
const port = 3000;

const server = http.createServer(function(req, res){
   // Everything Here
   res.write('Hello Node');
   res.end()
})

server.listen(port, function(error){
   if(error){
       console.log('Something went wrong', error)
   } else {
       console.log('Server is listening on port' + port)
   }
})

The module http is Node core module no need to install. Importing in project totally works fine. However other third party packages/module need to download before importing them in the project.

Blogger Nepal