Know the difference between Express Server Vs Http Server and express.json() Vs bodyparser.json() !!

Know the difference between Express Server Vs Http Server and express.json() Vs bodyparser.json() !!

Before going to learn the difference between http server and express server, express.json() and bodyparser.json() , let's first define the express in simple terms...

Express.js is a web framework of Nodejs that provides lots of features & simplicity to the code

So express.js helps the developer and says

"Hey!! I am there to help you, do not write so much of code for single thing"

image.png

1. Difference between express server and http server:

Express and Http both are used to create the server in your Nodejs application, so the question is which to use then and why??

But before going to know the answer of above question, let's first understand how they works...

- How to create HTTP server ??

Http is an in-build module which is pre-installed along with Nodejs and we don’t need to install it explicitly. It means HTTP is an independent module. Below is the implementation:

     cont http = require("http");
     const server = http.createServer();
     server.listen(3000,()=>{
          console.log("Server started on port 3000 using HTTP");
     });

- How to create EXPRESS server ??

Express is a framework as a whole and we have to install it explicitly using npm command:

npm install express

Express is made on top of the HTTP module. Below is the implementation:

     const express = require("express");
     const app = express();
     app.listen(3000,()=>{
          console.log("Server started on port 3000 using EXPRESS");
     });

image.png

Imp Note: Express uses the Https module internally and provides lots of features and simplicity along with creating the server. You can use both Http and Express to create your server

2. Difference between express.json() and bodyparser.json():

express.json() and bodyparser.json(), both are used to convert the incoming request into json so that request can be access into json type which is more feasible and accepted format.

Just like we preferred express server rather than http server which gives lots of features along with creating the server, same thing is happening here also. You can give analogy that http server is just same as the bodyparser.json() not in working and implementation but in relation.

So, now let's understand their implementation also:

image.png

- bodyparser.json()

bodyparser is external module of express and we have to install it explicitly using npm command:

npm install body-parser

     const express = require("express");
     const app = express();

     const bodyParser=require("body-parser");
     app.use(bodyParser.json());     
     app.use(bodyParser.urlencoded({
        extended: true
     }));

     app.listen(3000,()=>{
          console.log("Server started on port 3000 using EXPRESS");
     });

- express.json()

     const express = require("express");
     const app = express();

     app.use(express.json());
     app.use(express.urlencoded({ extended: true }));

     app.listen(3000,()=>{
          console.log("Server started on port 3000 using EXPRESS");
     });

Note: Here if you see, we are using urlencoded, basically it is an extra thing which we are adding because URL encoding is a mechanism for translating unprintable or special characters to a universally accepted format by web servers and browsers

Imp Note: Just like express uses http internally, same here express uses bodyparser internally. You can use anything but bodyparser is depreciated in latest version of express

If you like this article, Plz like and comment !!

image.png

Did you find this article valuable?

Support Anubhav Goyal by becoming a sponsor. Any amount is appreciated!