Mastering requests
In Chapter 9, we learned all the theory around HTTP requests and responses. Here, we will cover how to handle these with Express.
In this section, we are going to focus on this snippet of pseudo-code:
import express from ('express') const app = express() app.method(route, handler)
We have three elements here to understand:
method
, that is, the HTTP method that we want to handle, for example,GET
,POST
,PUT
,DELETE
, and so onroute
, that is, the path that we want to handle, for example,/
,/users
, or/users/:id
handler
, that is, the callback function that will be executed whenmethod
androute
match
HTTP methods
Express provides a method for each HTTP method. There are many out there (get
, post
, put
, head
, delete
, options
, trace
, copy
, lock
, mkcol
, move
, purge
, propfind
, proppatch
, unlock
, report
, mkactivity
, checkout
, merge
, m-search
, notify
, subscribe
, unsubscribe
, patch
, search
, and connect
).
The most common are get...