Whatâs express js
Express.js is a small framework that works on top of Node.js 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.js HTTP objects and facilitates the rendering of dynamic HTTP objects.
Â
How it works
Schema :
Â

Why express :
â
Easy and Flexible routing system.
â
Integrates with many templating engines very easily, which is great when it comes to crafting frontend to our applications.
â
Contains a middleware framework
Â
Node.js is a popular JavaScript runtime that allows developers to write server-side applications in JavaScript. Express is a minimalist web framework built on top of Node.js that makes it easier to develop web applications by providing a set of tools and utilities. In this blog post, we will explore the basics of Express and Node.js and how they work together to build web applications.
Getting Started with Node.js and Express
To get started with Node.js and Express, you will first need to install Node.js on your machine. Once you have Node.js installed, you can use the Node Package Manager (npm) to install Express by running the following command :
npm install express --save
Â
This will install the latest version of Express and its dependencies. Once you have Express installed, you can create a new Node.js project and initialize it with npm by running the following
Â
This will install the latest version of Express and its dependencies. Once you have Express installed, you can create a new Node.js project and initialize it with npm by running the following commands:
mkdir myapp cd myapp npm init
Â
This will create a new directory called "myapp" and initialize a new Node.js project in that directory. You will be prompted to answer a few questions about your project, such as the name, version, and description.
Creating a Simple Express Application
Once you have your project set up, you can create a simple Express application by creating a new file called "app.js" and adding the following code:
Â
const express = require('express') const app = express() app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(3000, () => { console.log('Server listening on port 3000') })
Â
This code creates a new Express application and defines a route for the root URL ("/"). When a GET request is made to this route, the server responds with the message "Hello World!". The server is then started and listens on port 3000.
To run the server, you can execute the following command:
node app.js
Â
This will start the server and you should be able to access it by navigating to http://localhost:3000 in your web browser. You should see the message "Hello World!" displayed in the browser.
Â
Routing in Express
In the example above, we defined a single route for the root URL ("/"). However, Express allows you to define multiple routes for different URLs. For example, you could define a route for "/about" by adding the following code:
app.get('/about', (req, res) => { res.send('About Us') })
Â
This code defines a new route for the URL "/about". When a GET request is made to this route, the server responds with the message "About Us".
You can also define routes that accept parameters by using a colon (":") followed by the parameter name. For example, you could define a route for "/users/:id" by adding the following code:
app.get('/users/:id', (req, res) => { const userId = req.params.id res.send(`User ${userId}`) })
Â
This code defines a new route for the URL "/users/:id". When a GET request is made to this route, the server extracts the value of the "id" parameter from the URL and responds with a message that includes the value of the parameter.
Middleware in Express
Express also provides middleware, which are functions that can be executed before or after a route is handled. Middleware functions can perform tasks such as logging, authentication, and error handling.
Â
For example, you could add a middleware function that logs the time and date of each incoming request by adding the following code:
Â
app.use((req, res, next) => { const now = new Date() console.log(`${now.toLocaleString()} -