Open In App

What is Express?

Last Updated : 25 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Express is a small framework that works on top of Node web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. It adds helpful utilities to Node HTTP objects and facilitates the rendering of dynamic HTTP objects.

Prerequisites:

Features of ExpressJS

1. Routing

Routing is the process of handling an HTTP request that defines which kind of response will be sent to the client on which particular request. In Node JS, we have a module called http to create a server, where we create a server using http.createServer and pass a callback function to http.createServer, where we get requests and responses as s parameter and using if else and URL, we setup routes.

2. Middlewares

Middlewares are the middle processes that execute between processes. In terms of web development, when we store passwords in a database using a server, we use middleware to encrypt our passwords to make them secure. But Node JS does not contain any middleware by default, but we can create our own custom middleware in it. Instead of Node JS, Express.js contains built-in middleware like express.static() to server static files to clients.

3. Error Handling

Error handling is used to ensure the smooth running of your software or program in case of any undetected issue in your software. But in Node JS, there is no way to handle errors automatically through any module. Developers can setup error handling using try catch blocks or event emitters. But in Express JS, it is much easier to handle errors because there are multiple ways to handle errors, like asynchronous handling, global error handling, etc.

4. Request & Response Object

Request and Response means what is requested by the client side and, in exchange for that request, what data is sent to the client side from the server side in terms of response. The request and response object is contained in both Node JS and Express JS, but still, Express JS comes with multiple additional functionalities in this object. For example, Express JS allows developers to use parameters to access URLs, and res.send() is a much more convenient way to send responses. It also allows user middlewares to be used in server-side coding.

5. Body Parsing

Body parsing refers to parsing data sent from the client side to the server side. The client sent data in the body of the request and also sent the type of content in headers, so converting data according to the content type is called body parsing. In Node, there is no built-in method or function to parse client-side data, but we can use modules like querystring or buffer. But Express JS contains built-in modules to parse data without any external modules like middleware or Express.json() Parsing.

Use Express in your Node Application

Step 1: In the existing Node Application install Express.

npm install express

Step 2: In the app.js file add the following code.

Javascript




//app.js
 
const express = require('express');
const app = express();
const PORT = 4000;
 
// Define a basic route
app.get('/', (req, res) => {
    res.send('Welcome To GeeksforGeeks!');
});
 
// Start the server
app.listen(port, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});


Step 3: To start the application run the following command.

node app.js

Output:

Screenshot-2024-01-23-122323

Output

Advantages of Express:

  • Simplifies web server development on NodeJS.
  • Offers a minimalist and flexible framework for scalability.
  • Enhances code readability and maintainability.
  • Accelerates development with a feature-rich API.
  • Facilitates a vibrant ecosystem for easy integrations.

Disadvantages of Express:

  • Limited support for real-time applications out of the box.
  • Asynchronous code can lead to callback hell (callback-based nested code).
  • Relatively steeper learning curve for beginners compared to simpler frameworks.
  • Express itself is unopinionated, which may require additional decision-making on project structure.
  • Less built-in functionality compared to larger frameworks like Django or Ruby on Rails.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads