Skip to main content

What is Express & how we use it .

Express: we usually heard express as expressjs and many people think that it as a library.

Lets  try to understand what is Express.




express is a middleware framework which is used to manipulate requests in a simple node application .
It is not a complete library instead its a middleware. so now we have a question what is a middleware exactly so the answer is a middle is a function which manipulates a data which come s to a server and pass it on to the next middleware.

When we talk about node , In node when a request comes to a server it goes through several middlewares and finally a response went back to the browser. 
You can read about middle wares in a different post.

Lets talk out express so how we use express in our node code.

first thing is prerequisites:
  1. Node
  2. Editor(VS code, notepad++)
  3. Internet Connection
I am assuming you are having a node app with a package.json file such that you have your node modules in place.

Lets install the express in our application .You can use below command to install express into your application.
npm install express

You can go through complete documentation using below links:
Once you run this command you will have a entry of express package  in your package.json file.

 
 Now let require this package to our server.js and start working on this . I believe you would be already knowing how to require a module in your application.


const express = require('express');

 usually if you are not using express then when you want to create a server you need to follow two steps :
  • Create a server 
  • Listen/bind the request  to the server.
but In case you are using Express you can simply listen to the request and in the background it will automatically creates a server.
first we need to crate an instance of express we usually name it as app.

const app = express();
app.listen(5000, () => {
console.log("Server has been started");
});

now let get to know how  we receive a  request in our express node server so we are use this app instance while trying to listen any request.

app.use("/", (req, res) => {
console.log(req);
res.send(200, "this is response");
});

so as in the above piece of code i tried to console the request but i will not be able to get the correct data from a request which has been sent to this particular server. 
Although it will work fine as it will send you the response back to the browser.

If you want to extract the data which you have sent to this server you can use body parser with express. 
you need to install body parser and then require it to your file with the help of express you can parse every request which comes to this server.

Now if you can see you have lot of valuable data which is currently present in req object .

Simillarly,  we can use express with multiple middle just to work easy . Express is used to simplify our life as coder.

Let have a complete code as well.

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser());
app.use("/", (req, res) => {
console.log(req);
res.send(200, "this is response");
});

app.listen(5000, () => {
console.log("req");
});


If you have any queries please comment ..

Comments

Popular posts from this blog

Basic of WorkFlow in Salesforce with Real Scenario Example

We live in a world increasingly trying to transform every component of it into a digital one. Business of all kinds are being built on digital platforms, some of them which until a few years ago had nothing to do with technology, are also willing to take the advantage of perks and flexibilities that a digital implementation would do in their business.