Express.js is a popular web framework for Node.js. It makes it easier to create web servers, websites, and REST APIs.
Express.js is a lightweight web framework built on top of Node.js. It provides useful features for handling HTTP requests, sending responses, creating routes, and building APIs.
Node.js: Allows JavaScript to run outside a browser.
Express.js: Makes building web servers and APIs with Node.js easier.
Express.js runs on Node.js. It does not replace Node.js.
Express.js helps developers build web applications with less code.
| Feature | Description |
|---|---|
| Routing | Handles different URLs and HTTP methods. |
| Request Handling | Reads information sent by clients. |
| Response Handling | Sends text, HTML, JSON, or other responses. |
| Middleware | Runs functions during the request-response process. |
| REST APIs | Helps create APIs for websites and mobile applications. |
You should know the following before starting Express.js:
Follow these steps to create your first Express project.
Open your terminal or command prompt and run:
mkdir express-demo
cd express-demo
Run the following command:
npm init -y
This command creates a package.json file
containing project information and configuration.
Run this command inside the project folder:
npm install express
npm downloads Express.js and adds it to the project's dependencies.
Create a file named app.js inside the
express-demo folder.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Welcome to Express.js!');
});
app.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
require('express') loads the Express package
using CommonJS.
express() creates an Express application.
app.get() defines a route for HTTP GET requests.
req represents the incoming request.
res represents the response sent to the client.
res.send() sends a response to the client.
app.listen() starts the server on port 3000.
Run the following command in your terminal:
node app.js
You should see this message:
Server running at http://localhost:3000
Now open your browser and visit: http://localhost:3000
Your first Express application is running successfully.
Routing determines how an application responds to a request made to a particular URL and HTTP method.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Home Page');
});
app.get('/about', (req, res) => {
res.send('About Page');
});
app.get('/contact', (req, res) => {
res.send('Contact Page');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
Try these URLs:
http://localhost:3000/ — Home Pagehttp://localhost:3000/about — About Pagehttp://localhost:3000/contact — Contact PageExpress.js is commonly used to create APIs. APIs often send data in JSON format.
const express = require('express');
const app = express();
app.get('/student', (req, res) => {
res.json({
id: 101,
name: 'Rahul',
course: 'Node.js'
});
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
Visit http://localhost:3000/student.
Expected JSON response:
{
"id": 101,
"name": "Rahul",
"course": "Node.js"
}
Middleware is a function that can run during the request-response cycle. Middleware can inspect requests, perform tasks, modify request or response objects, and pass control to the next function.
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log('A request was received');
next();
});
app.get('/', (req, res) => {
res.send('Hello Student!');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
app.use() registers middleware.
next() passes control to the next middleware
or route handler when appropriate.
| Method | Purpose |
|---|---|
express() |
Creates an Express application. |
app.get() |
Handles HTTP GET requests. |
app.post() |
Handles HTTP POST requests. |
app.put() |
Handles HTTP PUT requests. |
app.delete() |
Handles HTTP DELETE requests. |
app.use() |
Registers middleware or mounts a router. |
res.send() |
Sends a response. |
res.json() |
Sends a JSON response. |
app.listen() |
Starts the server listening for requests. |
| Node.js HTTP Module | Express.js |
|---|---|
| Built into Node.js. | Installed separately using npm. |
| Requires more manual request handling. | Provides convenient routing and middleware. |
Uses http.createServer(). |
Uses express() to create an app. |
| Useful for low-level HTTP server work. | Useful for web applications and REST APIs. |
Create an Express application with the following requirements:
my-express-app.npm init -y.app.js file./student route that returns JSON data.Select the correct answer for each question.