Handling Node.js Request Bodies With Micro

Learn how to parse Node.js request bodies with Micro for use inside serverless functions.

In this guide, we will show you how to parse a Node.js request body, for use inside a serverless function deployed to Now, without requiring a framework such as Express.

This guide assumes the request is sent with a Content-Type of application/json. However, many more content types can be parsed using the micro module.

Step 1: Creating the Function

To get started, create a project directory and cd into it.

Next, initialize the project with the following command:

yarn init

Initializing a project with Yarn.

After answering the questions given, you should add the only dependency required for this example, micro:

yarn add micro

Adding the micro dependency to the project.

To illustrate the parsing of a request body, create an index.js file at the root of your project directory with the following code:

const { json } = require('micro')

module.exports = async (req, res) => {
  const body = await json(req)
  res.end(`Hello ${body.name}, you just parsed the request body!`)

An example of how to parse a request body using Node.js and Micro.

This function takes a POST request, parses the body, and uses data from the body in the response.

The key part here is line 4. For those used to working with Express, it should be similar to accessing req.body.

json, imported from micro on line 1, is a function that returns the request body in the JSON format, fully parsed and ready to access inside your function.

This is illustrated in the response, as the JSON object is accessed to use the name property that was sent in the initial request.

Step 2: Deploying the Function

Deploying your function to Now is easy. First, create a now.json file in the root of your project directory with the following code:

  "version": 2,
  "builds": [{ "src": "index.js", "use": "@now/node" }]

An example now.json file in your project.

After creating your now.json file, you should deploy your project with just a single command:


Deploying your project with just a single command.

You have now created and deployed your project, all that's left to do is test that it works.

Step 3: Sending the Request

To verify that the JSON is being parsed correctly, make a POST request to your new deployment using curl by executing the below code inside your terminal:

curl -X POST "https://your-deployments-url.now.sh/" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Reader"

Making a POST request using curl.

Note: You should change the URL to match the one for your deployment given to you in the Now CLI.

You will receive a response similar to the following:

Hello Reader, you just parsed the request body!

An example response from making a POST request.

Congratulations, now you know how to parse request bodies in Node.js!

Bonus: Understanding Why this Works

When Node.js receives a request, the body is in the format of a ReadableStream.

To get the data from the stream, you need to listen to its data and end events. To do this requires a few lines of code which you would much rather not have to repeat.

By using micro, parsing request bodies is concise and straightforward as it handles everything for you, all you need to do is specify the format you want to receive the data in.

More Resources

For more information on using Node.js with Now, take a look at the @now/node builder.

To configure Now further, please see these additional topics and guides:

Written By
Written by msweeneydevmsweeneydev
on May 10th 2019