Request Processing

Tutorial 3 of 4

Request Processing with Express

1. Introduction

Welcome to this tutorial on request processing with Express. The goal of this tutorial is to help you understand how to handle and process requests effectively in an Express application. By the end of this tutorial, you will gain the skills to:

  • Interpret request data
  • Perform appropriate actions based on request data
  • Handle errors during request processing

Prerequisites: Basic understanding of JavaScript and Node.js is required. Familiarity with Express.js would be a plus, but not necessary.

2. Step-by-Step Guide

The Express application is essentially a series of middleware function calls. Middleware functions have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.

Concepts to Understand

  • Request Object: The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.

  • Middleware Functions: Middleware functions are functions that have access to the req and res objects, and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

  • Error Handling: If an error is thrown in a middleware function, Express will skip the remaining route callbacks and middleware, and go directly to the first error handling middleware.

Best Practices

  • Always end the request-response cycle in the middleware function, unless you want to pass it to the next one.
  • Handle all errors that occur while running route handlers.

3. Code Examples

Example 1: Handling GET Request

const express = require('express');
const app = express();
const port = 3000;

// GET method route
app.get('/', function (req, res) {
  // Send a response
  res.send('GET request to the homepage');
});

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});

In this example, we have a single route (/) that handles GET requests. When making a GET request to the root of our app, it will send back the string 'GET request to the homepage'.

Example 2: Handling POST Request

const express = require('express');
const app = express();
const port = 3000;

// POST method route
app.post('/', function (req, res) {
  // Send a response
  res.send('POST request to the homepage');
});

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});

This example is similar to the previous one but it handles POST requests. When a POST request is made to the root of our app, it will respond with 'POST request to the homepage'.

4. Summary

In this tutorial, we covered the fundamentals of request processing in Express, how to handle different types of HTTP requests, and the importance of error handling. The next step would be to learn about how to use Express middleware for things like logging, authentication, etc. Some good resources to continue your learning are the official Express.js website and Mozilla Developer Network (MDN).

5. Practice Exercises

Exercise 1: Create an Express app that responds with 'Hello World!' to a GET request at the root ('/').

Solution:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});

Exercise 2: Modify the above app to respond 'Hello Express!' to a POST request at the root ('/').

Solution:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Hello World!'));
app.post('/', (req, res) => res.send('Hello Express!'));

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});

Tips for further practice: Try to handle more types of HTTP requests, like PUT and DELETE. Also, learn how to handle requests at different routes.