How to do it...

Let's see what it takes to access the parameters. First, you have to require the body-parser module and ask for the options you want; we'll get into that next:

// Source file: src/get_parameters.js

const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));

Since you want the parameters to be parsed before any processing, the app.use() line will be at the top of your stack.

Now, getting into more detail, the body-parser module provides four parsers:

  • A URL-encoded body parser, just like we're using here, to read about the differences in using extended true or false. Checkout https://github.com/expressjs/body-parser for more information.
  • A JSON body parser, as in bodyParser.json(), to process requests with Content-Type is done through application/json.
  • A raw body parser, as with bodyParser.raw(), to process application/octet-stream contents by default, though this can be changed by providing a type option.
  • A text body parser, like bodyParser.text(), to process text/plain content.

The three latter parsers may provide extra options; check the documentation for more on that. Note, however, that if you have to deal with multipart bodies, then you cannot rely on a body-parser; see https://github.com/expressjs/body-parser for some alternatives, and see what suits you.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset