DEV Community

Otoniel Reyes
Otoniel Reyes

Posted on

Express 10 Days guide | Day 2 - HTTP Methods & Middleware Functions

Day 2: HTTP Methods & Middleware Functions

Introduction

I'm sure these are two of the most important members to build an express application. Let's start digging on middleware functions.

Middleware Functions

Middleware functions are simple JavaScript functions that will be invoked as callbacks by our application. A middleware function can accept up to three parameters: a request object, a response object and the express' next method.

In the last guide I wrote all middleware functions using both three but just use 2 of them. Now is time to explore them.

req

The first object express pass through our middleware is a request object. This contains useful properties and methods about the performed request, like the sent parameters, the request's body, etc.

To access all this info, you just need to call it as any object properties:

function(req, res, next) {
  console.log(req.body);
  console.log(req.params);
  next();
}
Enter fullscreen mode Exit fullscreen mode

res

The next one is the response object, very helpful too. The response object is what we use to send responses back to the client.

Until now, we know the respose's send method, other methods are json, and download, which we'll see in future guides.

An example here:

function(req, res, next) {
  res.json([{name: 'John', lastName: 'Doe'}])
}
Enter fullscreen mode Exit fullscreen mode

next

Maybe the simplest to understand is the next method. This is used to go forward in the express life cycle.

An Express application follow a fixed life cycle structure, from start to finish. What we say when we execute the next method inside a middleware function is, "we're done here, go to the next section". Then Express look for another middleware function to execute.

Maybe right now you cannot see the importance of this method, but, in the 4th guide we'll build some middleware, and then you'll see how to use it properly.

HTTP methods

All HTTP methods have an implementation in express, so, we can teach our application how to work with each type of request and data we receive from the client, how to process it and how to send back a proper response.

When we build a web server, we can restrict some URL, ask for a token to grant access to it or just watch a specific HTTP request to send a message or anything. Let's see this:

app.get('/', function(req, res, next) {}) // this looks for a GET request
app.post('/', function(req, res, next) {}) // this looks for a POST request
app.put('/', function(req, res, next) {}) // this will look for a PUT request
app.patch('/', function(req, res, next) {}) // this will look for a PATCH request
app.delete('/', function(req, res, next) {}) // and this one will look for a DELETE request
Enter fullscreen mode Exit fullscreen mode

With all these, our application is now capable of response to all those methods, now all we have to do is to process the proper response and send it back.

Coming next

In the next guide we're going to build our first API to practice all we already know and will add some extra express features to our app.

Something missed?

If you think there is something important that I don't talked about here, or have recommendations, just leave a message down below.

Top comments (0)