Facebook Pixel

How to Do Manual Routing in Node.js Without Express

Routing without Express teaches how servers work. Here is how to do it manually.

How to Do Manual Routing in Node.js Without Express

Routing without Express teaches how HTTP servers work. Here is how to do it manually.

Parse the URL

Use request.url to get the path. Use the URL module to parse it into pathname and query. This gives you the route and the query parameters.

Check the Method

request.method is 'GET', 'POST', 'PUT', or 'DELETE'. Combine the URL and method to determine which route handler to call.

A Simple Switch or If

If path is '/api/users' and method is 'GET', call getUsers. If path is '/api/users' and method is 'POST', call createUser. This is manual routing, which Express formalizes with app.get and app.post.

Handle 404s

If no route matches, send a 404 response. This is what Express does with its default error handler when no route matches.

Extract Route Parameters

For routes like /api/users/:id, parse the id from the path manually. Express does this with its router, but with the raw http module, you split the path and extract the id.

Why Do This Manually

It teaches how routing works under the hood. Express abstracts this, but understanding it manually makes Express more meaningful and helps you debug routing issues.

The Takeaway

Do manual routing by parsing request.url with the URL module, checking request.method, using a switch or if for route matching, handling 404s, and extracting route parameters by splitting the path. This teaches how Express works under the hood.

Parse request.url with the URL module, check request.method, use a switch or if to match routes, handle 404s for unknown routes, and extract route parameters by splitting the path. This is manual routing that Express formalizes.

Because it teaches how HTTP routing works under the hood. Express abstracts this, but understanding it manually makes Express more meaningful and helps you debug routing issues at a deeper level.

Use the URL module: new URL(request.url, 'http://localhost').searchParams gives you a URLSearchParams object with get and getAll methods for query parameters.

Split the path and extract the id manually. For /api/users/123, split by '/' and take the fourth element. Express formalizes this with its router, but with the raw http module you do it yourself.

If no route matches after checking all your routes, send a 404 response with response.writeHead(404) and response.end('Not Found'). This is what Express does with its default error handler when no route matches.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.