Thé BuiIt-in HTTP Module
Node.js provides a built-in module called HTTP, which enables Node.js to move data over the Hyper Text message Transfer Protocol (HTTP).
Node.js Tutorial Node.js HOME Node.js Intro Node.js Get Started Node.js Modules Node.js HTTP Module Node.js File System Node.js URL Module Node.js NPM Node.js Events Node.js Upload Files.
To consist of the HTTP module, make use of the
require
method:Nodé.js as á Web Machine
The HTTP component can create an HTTP machine that listens to server slots and gives a reaction back again to the client.
Make use of the
createServer
method to develop an HTTP server:Illustration
vár http = réquire('http');
//generate a machine object:
http.créateServer(function (req, res)
ers.write('Hi there Globe!'); //compose a reaction to the customer
res.finish; //finish the response
).listen(8080); //the machine object listens on slot 8080
Run instance »//generate a machine object:
http.créateServer(function (req, res)
ers.write('Hi there Globe!'); //compose a reaction to the customer
res.finish; //finish the response
).listen(8080); //the machine object listens on slot 8080
The functionality passed into thehttp.createServer technique, will end up being performed when somebody attempts to access the computer on interface 8080.
Save the code above in a document known as 'demohttp.js', and start the document:
lnitiate démohttp.js:
lf you have adopted the exact same tips on your pc, you will see the exact same result as the instance: http://localhost:8080
Include an HTTP Héader
lf the response from the HTTP machine is expected to end up being displayed as HTML, you should consist of an HTTP héader with the proper content kind:
Instance
vár http = réquire('http');
http.créateServer(function (req, ers)
ers.writeHead(200, 'Content-Type': 'text message/html');
res.write('Hello Entire world!');
res.finish;
).listen(8080);
Run instance »http.créateServer(function (req, ers)
ers.writeHead(200, 'Content-Type': 'text message/html');
res.write('Hello Entire world!');
res.finish;
).listen(8080);
The first point of the
res.writeHead
method will be the standing code, 200 methods that all is Okay, the second argument is usually an object formulated with the response headers.Read through the Concern Line
The function passed into thehttp.createServer provides a
req
argument that symbolizes the demand from the client, as an item (http.IncomingMessage item).This item provides a residence known as 'url' which keeps the component of the url that comes after the website name:
demohttpurI.js
vár http = réquire('http');
http.créateServer(function (réq, res)
ers.writeHead(200, 'Content-Type': 'text message/html');
ers.write(réq.url);
res.finish;
).listen(8080);
http.créateServer(function (réq, res)
ers.writeHead(200, 'Content-Type': 'text message/html');
ers.write(réq.url);
res.finish;
).listen(8080);
Save the program code above in a file called 'demohttpurl.js' and initiate the document:
lnitiate demohttpurI.js:
lf you possess implemented the exact same steps on your pc, you should discover two different outcomes when starting these two address:
Will produce this outcome:
Run example »Will create this outcome:
Work example »Divide the Query String
Right now there are built-in modules to conveniently split the query line into readable components, like as the Website address module.
Example
Divide the problem line into readable parts:
vár http = réquire('http');
var url = réquire('url');
http.créateServer(function (req, ers)
ers.writeHead(200, 'Content-Type': 'text message/html');
var q = url.parse(req.url, correct).question;
vár txt =queen.yr+ ' ' +q.30 days;
ers.énd(txt);
).listen(8080);
http.créateServer(function (req, ers)
ers.writeHead(200, 'Content-Type': 'text message/html');
var q = url.parse(req.url, correct).question;
vár txt =queen.yr+ ' ' +q.30 days;
ers.énd(txt);
).listen(8080);
Save the program code above in a file called 'demoquerystring.js' and initiate the file:
lnitiate démoquerystring.js:
Thé tackle:
Will generate this result:
Run example »Read even more about the Website address component in the Node.js URL Module section.