Implement a Route Handler

Learn how to implement a route handler for POST.

We’ve already seen an example of a built-in middleware when we used the express.static() method earlier on to serve static assets to the client.

Route handler for POST

A common server task is to handle form data or JSON data submitted via a POST request. However, by default, Express doesn’t know how to handle certain types of requests, like a POST request that contains JSON data in the request body, unless we call specific middlewares.

Let’s take a look at two widely used built-in middlewares:

  1. Add the express.json() middleware to parse JSON data and make it accessible in the request object (via req.body). Also, add the express.urlencoded() middleware to parse incoming requests with URL-encoded payloads.

Get hands-on with 1200+ tech skills courses.