Express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.The most common use for Node.js is writing Web applications, and a large percentage of these web applications today are using Express.js as their server framework. Express can be used to create JSON APIs, Server-side rendered web applications, or Microservices. It has become the standard server framework for node.js. Express is the backend part of something known as the MEAN stack.
The MEAN is a free and open-source JavaScript software stack for building dynamic web sites and web applications which has the following components;
1) MongoDB โ The standard NoSQL database 2) Express.js โ The default web applications framework 3) Angular.js โ The JavaScript MVC framework used for web applications 4) Node.js โ Framework used for scalable server-side and networking applications.
The Express.js framework makes it very easy to develop an application which can be used to handle multiple types of requests like the GET, PUT, and POST and DELETE requests.
Developers :-
TJ Holowaychuk, StrongLoop and others, The initial release date of express is November 16, 2010.
What are Routes?
Routing aims to describe what code needs to be run in response to a request that the server received. This is typically done based on the combination of the URL pattern and the HTTP Method associated with the request.These are the basic building blocks of any API or web application, and Express provides us with flexible ways to define code to handle these requests.
for example:-
http://localhost:3000/Books
http://localhost:3000/Students
- If a GET request is made for the first URL, then the response should ideally be a list of books.
- If the GET request is made for the second URL, then the response should ideally be a list of Students.
- So based on the URL which is accessed, a different functionality on the webserver will be invoked, and accordingly, the response will be sent to the client. This is the concept of routing. Each route can have one or more handler functions, which are executed when the route is matched.
Request/Response
Often shortened to req and res, you'll see these everywhere when working with Express and other Node.js server frameworks. They represent the Request which was received by the server, and the Response which you will eventually send back.
Middlewares
Middleware are functions that have access to the Request and Response objects. They can run any arbitrary code, and are executed one after another according to what order the middleware are stacked. Middleware are part of the program that add some kind of functionality to your application, often by augmenting or adding things to the req and res objects themselves. Middleware can be added into your application without needing to change much.
Why use Express?
- Ultra fast I/O
- Asynchronous and single threaded
- MVC like structure
- Robust API makes routing easy.
Example code to import express in the node app:
const express = require('express');
Initializing the app:
const app = express();
Getting the path request and sending the response with text:
app.get('/', (req,res)=>{
res.send('Hi, your request has been received')
})
Listening at the port 2000:
app.listen(3000, ()=>{
console.log('listening at https://localhost:2000')
})
After running this JavaScript file in the terminal by command "node nameOfFileHere", the server is started and if in the browser's URL "http://localhost:3000" is typed, the text "Hi, your request has been received" can be seen.
Companies using Express in their stack:
- stack
- accenture
- client platform etc.
Alternatives of express:
Koa, React, Flask, Django, and Go are the most popular alternatives and competitors to ExpressJS.