Quiz Entry - updated: 2026.07.05
How do you create a basic Express route handler?
Call a method like router.get() with a URL path and a callback that receives (req, res) and sends the response.
import express from 'express';
const router = express.Router();
// Handle GET requests to '/'
router.get('/', function(req, res) {
res.send('Hello World!');
});
export default router;
Components:
- Import Express and create a router
- Define routes with HTTP method + path + handler
- Export the router for use in main app
Request: GET http://localhost/hello
Response: Hello World!
Multiple routes in one file:
router.get('/users', handleGetUsers);
router.post('/users', handleCreateUser);
router.get('/users/:id', handleGetUser);
Go deeper:
Express: Routing guide — route methods, paths,
:idroute params, andexpress.Router, official docs.