How to create HTTP Server in node js
In this chapter, you will learn how to create HTTP Server in node js. One of the most powerful futures of the npm is it provides the ability to create an HTTP Server.
That can be used to communicate between the different channels like web or mobile or IoT. Node js has the ability to create the HTTP server with different ports.
Let’s create an HTTP server and see the browser
Create a file: server.js and put the below
Run the command:
node server
Solution 1:
We can use the node js core module HTTP to create the HTTP server. After importing the HTTP core module you can use the createServer() method.
//import http module var http = require('http'); //import http module var http = require('http'); // create a server http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); console.log('HTTP Server has been started at port 7000....'); res.write('HTTP Server has been started....'); res.end(); }).listen(7000); //output HTTP Server has been started at port 7000.... HTTP Server has been started....
Output in browser
One thought on “How to create HTTP Server”
Comments are closed.