Unique features of Express JS - GeeksforGeeks (2024)

Last Updated : 23 Nov, 2023

Summarize

Comments

Improve

Express.js is known for its simplicity and flexibility which comes with various features that helps you to build efficient and scalable web applications. It makes it easier to organize your application’s functionality with middleware and routing. It adds helpful utilities to Node.js HTTP objects and facilitates the rendering of dynamic HTTP objects.

Let’s dive into some of the unique features of Express.js along with its examples.

Table of Content

  • Routing
  • Middlewares
  • Error Handling
  • Body Parsing

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.

Node JS:

if (method === 'GET' && url === '/') {
res.end('Hello, World!');
}

While in Express JS, routing and creating servers is an inbuilt feature, we don’t need to setup if else statements to setup routes. We can directly use the simple methods of Express JS to setup routes.

Express JS:

app.get('/', (req, res) => { res.send('Hello, World!'); });

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.

Syntax:

app.use(express.static('public')); 

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.

Syntax:

 throw new Error('Error');

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.

Syntax:

app.get('/', (req, res) => {
console.log(req.method);
console.log(req.url);
res.send('Hello, World!');
});

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.js, 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.

Syntax:

app.use(express.json()); 

Example: Implementation of above features.

Javascript

const express = require('express');

const app = express();

const port = 3000;

app.use(express.json());

app.use((req, res, next) => {

console.log(`MiddleWare Accepted`);

next();

});

app.get('/', (req, res) => {

res.send('Hello, World!');

});

app.post('/test', (req, res) => {

console.log(req.body);

res.send("Data Recieved!")

});

app.use((err, req, res, next) => {

console.error(err.stack);

res.status(500).send('Something went wrong!');

});

app.listen(port, () => {

console.log(`Server Established on Port -> ${port}`);

});

Output:

Unique features of Express JS - GeeksforGeeks (1)



Unique features of Express JS - GeeksforGeeks (2)

Improve

Please Login to comment...

Unique features of Express JS - GeeksforGeeks (2024)
Top Articles
Buy Or Sell Mandela Coins To The Highest Bidder | bobshop.co.za | bobshop.co.za
REST APIs vs Microservices: Key Differences
Where are the Best Boxing Gyms in the UK? - JD Sports
Dollywood's Smoky Mountain Christmas - Pigeon Forge, TN
Here are all the MTV VMA winners, even the awards they announced during the ads
My Boyfriend Has No Money And I Pay For Everything
Costco in Hawthorne (14501 Hindry Ave)
Irving Hac
Snowflake Activity Congruent Triangles Answers
Missing 2023 Showtimes Near Lucas Cinemas Albertville
Globe Position Fault Litter Robot
2135 Royalton Road Columbia Station Oh 44028
Yesteryear Autos Slang
Leeks — A Dirty Little Secret (Ingredient)
Craigslist Deming
Google Feud Unblocked 6969
Spoilers: Impact 1000 Taping Results For 9/14/2023 - PWMania - Wrestling News
Craigslist Clinton Ar
Brbl Barber Shop
Www Va Lottery Com Result
Disputes over ESPN, Disney and DirecTV go to the heart of TV's existential problems
Danielle Ranslow Obituary
Walmart Pharmacy Near Me Open
Rs3 Bring Leela To The Tomb
Tripcheck Oregon Map
Star News Mugshots
Why Are The French So Google Feud Answers
Advance Auto Parts Stock Price | AAP Stock Quote, News, and History | Markets Insider
Tmj4 Weather Milwaukee
Shoreone Insurance A.m. Best Rating
The 50 Best Albums of 2023
Usf Football Wiki
The Syracuse Journal-Democrat from Syracuse, Nebraska
Publictributes
Wasmo Link Telegram
Amc.santa Anita
Alston – Travel guide at Wikivoyage
Luciane Buchanan Bio, Wiki, Age, Husband, Net Worth, Actress
Fedex Passport Locations Near Me
Studentvue Calexico
Jammiah Broomfield Ig
Maplestar Kemono
5103 Liberty Ave, North Bergen, NJ 07047 - MLS 240018284 - Coldwell Banker
Pas Bcbs Prefix
Egg Inc Wiki
How to Do a Photoshoot in BitLife - Playbite
Msatlantathickdream
Naughty Natt Farting
Obituary Roger Schaefer Update 2020
Ok-Selection9999
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6044

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.