What are the differences between HTTP module and Express.js module ?
HTTP and Express both are used in NodeJS for development. In this article, we’ll go through HTTP and express modules separately
HTTP: It is an in-build module which is pre-installed along with NodeJS. It is used to create server and set up connections. Using this connection, data sending and receiving can be done as long as connections use a hypertext transfer protocol.
Example: Creating a server using the HTTP module in NodeJS.
index.js
// Importing http module var http = require( 'http' ); // Create a server object which listens on port 300 http.createServer( function (req, res) { // Write a response to the client res.write( 'Hello World!' ); // End the response res.end(); }).listen(3000); |
Run the index.js file using the following command.
node index.js
Output:
Express: Express as a whole is known as a framework, not just as a module. It gives you an API, submodules, functions, and methodology and conventions for quickly and easily typing together all the components necessary to put up a modern, functional web server with all the conveniences necessary for that (static asset hosting, templating, handling CSRF, CORS, cookie parsing, POST data handling, and many more functionalities.
Module Installation: You can install the express module using the following command.
npm i express
Example: Creating a server using the express module in NodeJS.
index.js
// Importing express const express = require( 'express' ); // Creating instance of express const app = express(); // Handling GET / Request app.get( '/' , function (req, res) { res.send( "Hello World!, I am server created by expresss" ); }) // Listening to server at port 3000 app.listen(3000, function () { console.log( "server started" ); }) |
Run the index.js file using the following command.
node index.js
Output:
Difference between HTTP module and Express.js module:
HTTP |
Express |
HTTP comes inbuilt along with NodeJS that is, we don’t need to install it explicitly. | Express is installed explicitly using npm command: npm install express |
HTTP is not a framework as a whole, rather it is just a module. | Express is a framework as a whole. |
HTTP does not provide function for static hosting, you require to write your own. | Express provide express.static function for static asset hosting. Example: app.use(express.static(‘public’)); |
HTTP is an independent module. | Express is made on top of the HTTP module. |
HTTP module provides various tools (functions) to do things for networking like making a server, client, etc. | Express along with what HTTP does provide many more functions in order to make development easy. |