Express JS

 What is Express JS

  • Express js is Node.js framework 
  • One of the most popular framework of Node.js available in the market.
  • Express.js is a web application framework that helps in creating web applications with the help of Node.js. 

Why we need Express.js over Node.js 

Express.js makes things easy for developers to create a web application. We can do all the things with Node.js that we can do with Express.js but In Node.js we have raw packages to create a server and all the things related to setup and running a server but in Express.js most of the things are handled by Express.js and developers have to only write the main logics to create his web application 

How to Install Express.js

  • As Express.js is NPM package. You can directly install it from npm command i.e.
    • npm install express
  • Before installing the express it is recommended to install the package.json using command so that you can keep track of different packages.
    • npm init 
  • No as per the package.js you must have set a file as main (generally it is Index.js or app.js).
  • this main file will be your entry point for the application. 

Run your first program in Express.js

As discussed above while installing the npm init you must have set a main file (generally it is app.js or index.js). The main file is our entry point of the application. To use the express package first import the express to your file and invoke the express() function. 

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

Express.js is user to work as server, so for each incoming request coming to express server it will return the response. For example if we have incoming request from route "/Home" so corresponding to this request server must return a response. Lets see how we can handle this in Express.

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

//Handling the route 
app.get("/Home",(req,res)=>{
    res.send("Hello world from express about");
});

//Server listening/Runinng 
app.listen(8000,()=>{
    console.log("Listing the port at 8000");
})

This is how this will work

0 Comments