Creating http Route Handlers

Picture a bustling train station where each platform (route) takes you to a different destination. In a Node.js server that uses the http package, each “platform” is a route—a combination of a specific HTTP method (like GET or POST) and a URL path (like / or /cat). When a request arrives at your server, your job is to figure out which platform it belongs to and then send a response designed just for that route.

By the end of this tutorial, you’ll know how to:

Defining a Route Handler

A route handler is a set of instructions for what your server should do when it receives a specific request. In Node’s http package, you identify a route by checking the request’s method (GET, POST, etc.) and url. Think of it like a train station where you look at the request’s ticket—does it say GET / or POST /cat? Based on that, you direct the request to the correct “platform” (code block) and hand out the right “response” (like “Splash Page” or “Created a Cat!”).

Here’s an example of how you might handle a simple route for GET /. If you spot a GET request to /, you give back a status code of 200 and plain text saying “Splash Page.”

const http = require('http');

const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/') {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    return res.end('Splash Page');
  }
});

server.listen(3000, () => {
  console.log('Server listening on port 3000');
});

In this code, the conditional ensures we only send the “Splash Page” if both req.method is GET and req.url is /.

Multiple Route Handlers

Most servers deal with many possible routes. When you add more “tracks,” you can direct different requests to different route handlers. For example:

const http = require('http');

const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/') {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    return res.end('Splash Page');
  }

  if (req.method === 'POST' && req.url === '/cat') {
    res.statusCode = 201;
    res.setHeader('Content-Type', 'text/plain');
    return res.end('Created a Cat!');
  }

  // If no route matches, you might send a 404
  res.statusCode = 404;
  res.setHeader('Content-Type', 'text/plain');
  return res.end('Route not found.');
});

server.listen(3000, () => {
  console.log('Server listening on port 3000');
});

Notice the use of return res.end(...) inside the if blocks. Once you call res.end(), your response is finished and sent back to the client. You typically return immediately after ending the response so the server doesn’t accidentally attempt to process more instructions or send a second response for the same request.

In real-world apps, you might have routes like:

The possibilities are endless. By checking req.method and req.url, you can handle each scenario differently.

What You've Learned

In this article, you saw how to direct traffic in your Node.js server by setting up route handlers—simple if checks that look at the request method and URL path. Each matching route returns a custom status code, headers, and response body. This is the first step toward building more robust applications that can handle many different types of requests.

Think of these route handlers like the various stops on a train line: each one leads to a different response or destination. As your server gets more complex, you’ll add more “stops” (routes) for clients to explore, each returning the unique data or actions your application needs.