Unopinionated minimalist web framework for NodeJS.

Hello world

const express = require('express');
const app = express();
const port = 3000;
 
app.get('/', (req, res) => {
	res.send('Hello World');
})
 
app.listen(port, () => {
	console.log(`Express app listening on port ${port}`);
})

HTTP Methods - GET, POST, PUT, DELETE, PATCH

There are many more methods as options, merge, purge, search, etc.

Dynamic Routing

  • Dynamic routing means, when the path can be vary, dynamic routes gives a way to get the value of the dynamic part.
  • For example route /user/443 the part user-id 443 is dynamic and depends on the user’s id, so we cant create hardcoded paths for all possible ids.
  • /user/:userId here :userId becomes the dynamic parameter in the URL and the value can be accessed.
app.get('/users/:userId', (req, res) => {
	const userId = req.params.userId;
	res.send(`User ID is: ${userId}`);
});
  • Multiple dynamic parameters:
app.get('/products/:categoryId/items/:itemId', (req, res) => {
  const { categoryId, itemId } = req.params;
  res.send(`Category ID: ${categoryId}, Item ID: ${itemId}`);
});
  • Optional Parameters:
app.get('/users/:userId/:profileId?', (req, res) => {
  const { userId, profileId } = req.params;
  if (profileId) {
    res.send(`User ID: ${userId}, Profile ID: ${profileId}`);
  } else {
    res.send(`User ID: ${userId}, No profile ID provided`);
  }
});
  • Route Order Matters Express evaluates routes in the order they are defined, so more specific routes should be defined before dynamic routes to avoid potential conflicts. In the below example, if the dynamic route is places above the /users/all, then it will be never matched with /users/all
app.get('/users/all', (req, res) => {
  res.send('Get all users');
});
 
app.get('/users/:userId', (req, res) => {
  res.send(`Get user by ID: ${req.params.userId}`);
});

Query Parameters

  • Query Params are data that is sent as key value pair after the ? in the URL, and multiple values are separated by &
  • These values can be accessed as below:
app.get('/search', (req, res) => {
  const searchTerm = req.query.term;
  const page = req.query.page;
  
  res.send(`Searching for: ${searchTerm}, Page: ${page}`);
});

ExpressJs Router

  • A router object is an instance of middleware and routes. It is capable of performing middleware and routing functions.
  • A router can be mounted to express as a middleware function.

Benefits

  • A router helps is modularizing the code. Otherwise the complete route paths would be needed to define in the same server.js file.

userRouter.js

const express = require('express');
const router = express.Router();
 
router.use(authorizer); // Use a middleware mounted only on this router
 
router.get('/info/:id', () => {
	const id = req.params.id;
	const info = getInfo(id);
	
	res.send(`Info of the user ${info}`);
})

Server.js

const express = require('express');
const app = express();
const port = 3000;
 
const userRouter = require('userRouter.js');
 
app.use('/user', userRouter);
 
app.listen(port, () => {
	console.log(`Express app listening on ${port}`)
})