Express routing handles HTTP requests. Middleware functions execute during the request-response cycle.
Getting Started
What is Node.js?
Modules & NPM
Modules & NPM
Process & Environment
Process & Environment Variables
Caching & Performance
Redis Caching
GraphQL with Node.js
GraphQL Basics
Node.js
Intermediate
FREE
Routing & Middleware
Lesson 2 of 17
Intermediate
Interactive
Syntax
NODEJS
app.get('/users', getUsers); app.post('/users', createUser); app.use(logger); // middleware
Express Routes and Middleware
NODE
const express = require("express"); const app = express(); // Custom middleware const logger = (req, res, next) => { console.log(`${req.method} ${req.url}`); next(); }; app.use(logger); app.use(express.json()); // Route parameters app.get("/users/:id", (req, res) => { res.json({ id: req.params.id }); }); // Router const router = express.Router(); router.get("/posts", (req, res) => res.json([])); app.use("/api", router);
Practice
1
Exercise
Practice this concept.
Answer
Write the code as shown above.
Quick Quiz
1
What did you learn?
This covers the basics.
Interview Questions
It is a fundamental Node.js feature.