Express.js: Basic Request Routing

Express routing connects a request's path and HTTP method (like GET /) to a specific handler function. This is the core of any Express app, used to define API endpoints or handle form submissions. A common mistake is using the wrong method for a request.
Why it exists
Web applications need a predictable way to respond to different user actions. A request to view a homepage (GET /) is different from a request to submit a form (POST /contact). Routing provides the mechanism to distinguish these requests and delegate them to the correct piece of code.
The mental model
Think of your Express application as a reception desk. Each incoming request has a destination (the URL path, like /about) and a purpose (the HTTP method, like GET). Routing is the receptionist's directory that says, "A GET request for /about goes to the showAboutPage function." It maps a specific request type to a specific handler.
How it works
You define routes on an instance of your Express app, typically named app. The structure is app.METHOD(PATH, HANDLER). METHOD is a lowercase HTTP request method like get, post, put, or delete. PATH is the URL path on the server you want to match. HANDLER is a callback function that Express executes when a request matches both the method and the path. This function receives a request object (req) and a response object (res) used to send a response back to the client.
When to use it
Basic routing is the first thing you'll set up in any Express application. Use it to create API endpoints that return data (e.g., app.get('/api/users', ...)), serve a homepage, or handle user interactions like creating or deleting resources (e.g., app.post('/users', ...)).
When not to use it
While essential, relying only on basic routing can make large applications messy. If you have dozens of routes for a single resource (like "users"), you should organize them using express.Router() to group related routes into separate files for better maintainability. Basic routing is for simple apps or for defining top-level entry points for these routers.
One canonical example
A simple server might have a homepage and an endpoint to handle a POST request. The homepage route responds to a GET request at the root path.
app.get('/', (req, res) => {
res.send('Hello World!');
});A route to handle a new resource creation would use the POST method.
app.post('/', (req, res) => {
res.send('Got a POST request');
});Each route matches a unique combination of an HTTP method and a URL path.
Interview question
How does Express.js determine which specific handler function to execute for an incoming web request?
- a.By executing the first handler function defined in the application, regardless of method or path.
- b.By matching only the HTTP method of the request to a defined route.
- c.By matching only the URL path of the request to a defined route.
- d.By matching both the HTTP method and the URL path of the request.Correct
Why? this is the answer
Express routing connects a request's path and HTTP method to a specific handler function. It requires both components to uniquely identify and execute the correct handler, as stated in the card: 'Each route matches a unique combination of an HTTP method and a URL path.' Options A and C are incomplete, as they only consider one part of the matching criteria. Option A describes a general execution order, not the specific matching logic for a route.
Just read this? Test yourself on what you have been reading.
Read the original → expressjs.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on express — each one lists the topics its interview covers.
See open roles