Basic Routing

Tutorial 1 of 4

Introduction

Welcome to this tutorial where we will learn the basics of routing in Express.js. Our goal is to understand how to define routes, create route handlers and send responses back to the client.

By the end of this tutorial, you will be able to:
- Understand what is routing in Express.js.
- Define and handle different routes.
- Send responses back to the client.

Before we start, make sure you have the following prerequisites:
- Basic knowledge of JavaScript.
- Node.js and npm installed on your system.
- Basic understanding of how Express.js works.

Step-by-Step Guide

What is Routing?

Routing refers to how an application's endpoints (URIs) respond to client requests. In Express.js, routing takes the following structure: app.Method(Path, Handler), where:
- app is an instance of express.
- Method is an HTTP request method, in lowercase.
- Path is a path on the server.
- Handler is the function executed when the route is matched.

Defining Routes

To define a route, we use app.Method(), replacing Method with the HTTP method we want to respond to. For example, to respond to GET requests, we would use app.get().

Creating Route Handlers

A route handler is the function that gets executed when a route is matched. It takes the request and response objects as parameters. The request object (req) represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, etc. The response object (res) is used to send data back to the client in response to the request.

Code Examples

Example 1: Basic GET Request

Here's an example of a basic GET request to the home page ("/").

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

// Define a route handler for GET requests to the home page
app.get('/', function(req, res) {
  res.send('Hello, world!');
});

app.listen(3000, function() {
  console.log('App is listening on port 3000');
});

In the above example, we first import Express.js and create an app. We then define a route handler for GET requests to the home page ("/"). If a client sends a GET request to our server's home page, they will receive the response 'Hello, world!'. Finally, we start our server on port 3000.

Summary

In this tutorial, we learned the basics of routing in Express.js. We learned how to define routes, create route handlers, and send responses back to the client.

For further learning, you might consider exploring more complex route patterns, middleware, or how to handle POST requests.

Here are some additional resources:
- Express.js Routing Guide
- MDN Web Docs on HTTP Methods

Practice Exercises

  1. Define a route handler for GET requests to "/about" that responds with 'About Us'.
  2. Define a route handler for GET requests to "/contact" that responds with 'Contact Us'.

Solutions:
1. javascript app.get('/about', function(req, res) { res.send('About Us'); });
2. javascript app.get('/contact', function(req, res) { res.send('Contact Us'); });

Keep practicing with different routes and responses. Try adding more HTTP methods to your repertoire. Happy coding!