Creation of http server and its routing with ExpressJS in NodeJS
Posted By : Aftab Alam | 29-Aug-2019
Introduction
As every nodeJS developer is aware of that http server can be created easily and ExpressJS provides routing for the application which internally uses http server but what when anyone wants to create his/her own http server with ExpressJS routing in the application. It blogs will show to achieve the same.
1.1. Http Module:- Http is an in-built module which gives facility to the user to create own http server in the application.
1.2. ExpressJS:- ExpressJS is a NodeJS framewok whuch gives facility to the user to implement routing for the application. Express is also an in-built module which is basically used to provide routing for a application. express is an in-build module which is used to achive the same.
2. Code Implementation server creation
2.1. Http Server Creation:- The http server can be created using http server and the following code is implementation for the same.
var http = require("http");
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type":"text/plain"});
response.end();
});
server.listen(4000, function() {
console.log("Server is running at", server.address().address+" "+server.address().port);
});
In the above implementation, the http module is imported then http server is created using imported http module. The http server creation takes a callback function which receives two obejcts(request, response) as parameters. On every request, same callabck function is invoked. For different types of requests, we have to check explicitly within the callback function.
The listen method takes two parameters first one is port on which server is running and second one callback function which is invoked on the successfully launch of http server instance.
2.2. ExpressJS Routing:- The express in-built module is used to create routing as described below example
var express = require("express");
var app = express();
app.get("/home", function(req, res) {
res.end("This is from home");
});
app.listen(4000, function() {
console.log("Server is running on port 4000");
});
In the above implementation, the express module is imported which returns a function and using that function app object is returned which is used to define routing and launch and instance of the http server.
The listen method takes two parameters first one is port on which server is running and second one callback function which is invoked on the successfully launch of http server instance.
2.3. Creation of Http Server with ExpressJS routing:-
In the above two implementaions, both are working independently. What if somebody wants to create own http server and wish to provide routing by expressJS then he/she can go for the followings implementation provided below.
var http = require("http");
var express = require("express");
var app = express();
var server = http.createServer(app);
app.get("/home", function(req, res){
res.end("This is from home");
});
server.listen(4000, function() {
console.log("Server is running at", server.address().address+" "+server.address().port);
});
In the above implementation, express and Http modules are imported. The Http server is creating by passing app object rather than passing a callback function. The app can be used to define the routing which would be applicable to created Http server.
Conclusion:- The Http server can be created in combination with express routing which might be needed in any specific application.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aftab Alam
Aftab has worked on multiple technologies in front-end as well as in back-end.