Article image Creating a Basic REST API with NodeJS and ExpressJS: Defining Routes

10.4. Creating a Basic REST API with NodeJS and ExpressJS: Defining Routes

Page 69 | Listen in audio

Building a basic REST API with NodeJS and ExpressJS is a multi-step process. One of the most important steps is defining routes, which allow users to interact with your API in different ways. In this article, we will discuss how to define routes in your API using NodeJS and ExpressJS.

To begin with, it's important to understand what a route is. In simple terms, a route is a path or URL that users can access to interact with your API. For example, if you have an API that manages a list of tasks, you can have routes like '/tasks' to get all tasks, '/tasks/:id' to get a specific task, '/tasks' to create a new one task, and so on.

To define routes in your API, you need ExpressJS, a framework for NodeJS that simplifies the creation of APIs. To install ExpressJS, you can use npm (Node Package Manager), which is a tool that allows you to install packages NodeJS. You can install ExpressJS with the following command:

npm install express

Once ExpressJS is installed, you can start defining your routes. To do this, you first need to create an instance of Express and then use the methods provided by that instance to define your routes. Here is an example:


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

app.get('/tasks', (req, res) => {
  // Code to get all tasks
});

app.get('/tasks/:id', (req, res) => {
  // Code to get a specific task
});

app.post('/tasks', (req, res) => {
  // Code to create a new task
});

app.listen(3000, () => {
  console.log('API running on port 3000');
});

As you can see, Express provides methods like 'get', 'post', 'put' and 'delete' which correspond to HTTP verbs that users can use to interact with your API. Each method accepts a URL (or route) and a callback function that will be called when the route is accessed.

The callback function takes two arguments: 'req' and 'res'. 'req' is an object that contains information about the HTTP request made by the user, such as URL parameters, data sent in the request body, request headers, etc. 'res' is an object that you can use to send a response to the user.

For example, if you wanted to send a list of tasks to the user when he accesses the '/tasks' route, you could do something like this:


app.get('/tasks', (req, res) => {
  const tasks = [
    { id: 1, name: 'shopping' },
    { id: 2, name: 'Study for the test' },
    // ...
  ];

  res.json(tasks);
});

In the example above, 'res.json' is a method that converts the array 'tasks' into a JSON string and sends it to the user as a response.

Defining routes in your API is a crucial part of the process of creating an API with NodeJS and ExpressJS. With routes defined, users can interact with your API in a variety of ways, getting data, sending data, updating data, and deleting data. When defining your routes, remember to think carefully about which routes you need and what data you need to send and receive on each route.

Now answer the exercise about the content:

What is the role of the 'get', 'post', 'put' and 'delete' methods provided by ExpressJS in defining routes in an API?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Creating a Basic REST API with NodeJS and ExpressJS: Creating the Controllers

Next page of the Free Ebook:

70Creating a Basic REST API with NodeJS and ExpressJS: Creating the Controllers

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text