Node.js HTTP Module
Node.js Tutorial » Node.js HTTP Module
What is Node js HTTP module?
The HTTP module allows Node.js to receive and transfer data via Hyper Text Transfer Protocol (HTTP).
The HTTP module creates an HTTP server that listens to server ports and gives a response back to the client.
Syntax:
We can create a HTTP server with the help of http.createServer() method.
Node.js as a Web Server
The following example demonstrates how to use the Node.js Http module to create a web server.
// Create a server
http.createServer((request, response)=>{
// Sends a chunk of the response body
response.write('Welcome to Nodejs page!');
//write a response to the client
response.end(); //end the response
}).listen(1443); // Server listening on port 1443
The function passed into the http.createServer() method, will be executed when someone tries to access the computer on port 1443.
Save the code above in a file called "demo_test_http.js", and initiate the file:
Related subjects:
What are node.js modules?
How do I install node.js
Tags: Node.js HTTP Module: tutorial, get request, example, post, request, source, http server, import http module, install http module, derver, client
What is Node js HTTP module?
Is HTTP a core module of node js?
What is HTTP module used for?
How do I create a HTTP server and get data in node js?
Add an HTTP Header
If the response from the HTTP server should be displayed as HTML, you should include an HTTP header with the correct content type:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Welcome to Nodejs page!');
res.end();
}).listen(80);
res.writeHead() method is the status code, 200 means that all is OK
Read the Query String
This object has a property called "url" which holds the part of the url that comes after the domain name:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
res.end();
}).listen(80);
Save the code above in a file called "demo_test_http_url.js" and initiate the file:
Node.js HTTP Module: tutorial, get request, example, post, request, source, http server, import http module, install http module, derver, client
What is Node js HTTP module?
Is HTTP a core module of node js?
What is HTTP module used for?
How do I create a HTTP server and get data in node js?
Node.js HTTP Module - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.