RingoJS

Module middleware/params

This module provides middleware for parsing HTTP parameters from the query string and request body. It does not parse multipart MIME data such as file uploads which are handled by the upload module.

This installs a params() method in the application that accepts a configuration object with the following properties:

  • limit: maximum length of the request body in bytes, -1 disables the limit, defaults to 100 KiB
  • strict: if true (default), only accept JSON following RFC 4627, otherwise also parse pseudo-JSON like "1234"
  • reviver: optional function to transform the result JSON, see JSON.parse(str, reviver) for details

Example

const app = new Application();
app.configure("params", "route");
app.params({
  limit: 10,
  strict: true,
  reviver: function(key, val) {
    return (typeof val === "number") ? val * 2 : val;
  }
});

app.post("/example-json", function (req) {
  // JSON requests will parsed into req.postParams
  const jsonObj = req.postParams;
};

app.post("/example-form", function (req) {
  // submitted form params are parsed into req.postParams
  // for application/x-www-form-urlencoded requests
  const formFields = req.postParams;
};

middleware (next, app)

Middleware for parsing HTTP parameters. This module handles URL-endcoded form data transmitted in the query string and request body as well as JSON encoded data in the request body.

Parameters

Function next

the wrapped middleware chain

Object app

the Stick Application object

Returns

Function

a JSGI middleware function


request.params

An object containing the parsed HTTP parameters sent with this request.


request.postParams

An object containing the parsed HTTP POST parameters sent with this request. If the content type of the request is application/json, the middleware parses the body and stores the in request.postParams.


request.queryParams

An object containing the parsed HTTP query string parameters sent with this request.