AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js HTTP Module


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

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:

var http = require('http');

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.

var http = require('http');

// 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:

Initiate demo_test_http.js:
SELECT * FROM [Brazil Customers];

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:

var http = require('http');
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:

Example: demo_test_http_url.js
var http = require('http');
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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.