NodeJS is a server-side application development platform based on Google Chrome's V8 JavaScript engine. It allows developers to create fast and scalable web servers. One of the main features of NodeJS is the use of event-driven programming model, which is especially useful for applications that need high I/O and low CPU consumption.

Before diving into the concepts of routes and request handling, it is important to understand what APIs are. API is the acronym for Application Programming Interface, which in free translation means Application Programming Interface. APIs allow the interaction between different software in an easy and safe way, being a way to integrate systems, providing benefits such as data security, integration with other systems and task automation.

Routes

In the context of an API, a route is a path or URL where the API can be accessed. Each route represents a specific resource that the API exposes to its users. In NodeJS, routes are defined using the Express module, which is a framework for NodeJS that provides functionality for building web applications and APIs.

For example, a simple web server with two routes can be created as follows:

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

app.get('/', function(req, res) {
  res.send('Hello World!');
});

app.get('/api/users', function(req, res) {
  res.send('List of users');
});

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

In the code above, the first route is the root route ('/') and the second route is the '/api/users' route. The app.get() function is used to define the routes. The first argument is the route path and the second argument is a callback function that will be executed when the route is accessed.

Request Handling

The callback function takes two arguments: req and res. The req argument represents the request that was sent to the server and the res argument represents the response that the server will send back to the client.

The req object contains information about the request, such as URL parameters, data sent in the request body, request headers, and other information. The res object is used to send the response to the client. It has methods to send the response as a file, as a JSON, as text, among other formats.

For example, to send a JSON as a response, you can do this:

app.get('/api/users', function(req, res) {
  var users = [
    { id: 1, name: 'John Doe' },
    { id: 2, name: 'Jane Doe' }
  ];

  res.json(users);
});

In the code above, when the '/api/users' route is accessed, the server returns a JSON with a list of users.

To manipulate the data sent in the request body, you can use Express's body-parser middleware. The body-parser extracts the entire request body and places it in the req.body object. For example:

var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.post('/api/users', function(req, res) {
  var user = req.body;

  // Here you can add the user to the database

  res.json(user);
});

In the code above, when a POST request is sent to the '/api/users' route, the server extracts the body of the request and returns it as a response.

In short, routes are the paths that API users can access, and request handling is how the server handles requests that are sent to those routes. These are fundamental concepts for creating APIs in NodeJS.

Now answer the exercise about the content:

What is a route in the context of an API in NodeJS and how is it defined?

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

You missed! Try again.

Article image Basic concepts of NodeJS: Middleware and its application

Next page of the Free Ebook:

9Basic concepts of NodeJS: Middleware and its application

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou 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