Request Data
Query strings
Query strings values can be accessed using the query
property of the request context.
import { controller, get } from 'kikwit';
@controller
export class Page {
@get
echo(context) {
const offset = context.query.offset;
const limit = Math.min(context.query.limit, 50);
const categories = context.query.categories;
context.sendJSON({ offset, limit, categories });
}
}
A GET /page/echo?offset=15&limit=20&categories=laptop&categories=phablet
request to the echo
action above will return the following JSON document
{ offset: 15, limit: 20, categories: ['laptop', 'phablet'] }
By default the [querystring][querystring-package-url] package's [parse][querystring-package-parse-url]
function is used to parse the query string.
You can assign a global custom function for query parsing by setting the queryParser
configuration property.
This function should accept a string and return an object which will accessible via the Context's query
property:
E.g.
{
...,
queryParser: querystring.parse,
...
}
When the queryParser
configuration property is set to false
the query string is not parsed at all and the query
property is not set.
You also can also assign a custom parser to a specific controller or action only using the @queryParser
decorator.
import { controller, get, queryParser } from 'kikwit';
@controller
export class Page {
@queryParser(myParser)
@get
echo(context) {
const offset = context.query.offset;
const limit = Math.min(context.query.limit, 50);
const categories = context.query.categories;
context.sendJSON({ offset, limit, categories });
}
}
function myParser(str) {
let [offset, limit, categories] = str.split('-');
categories = categories.split(',');
return { offset, limit, categories };
}
A GET /page/echo?15-20-laptop,phablet
request
to the echo
action will return the following
{ offset: 15, limit: 20, categories: ['laptop', 'phablet'] }
Request body
The body of the request can be accessed using the body
property of the request context.
import { controller, post } from 'kikwit';
@controller
export class User {
@post
register(context) {
const username = context.body.username;
const gender = context.body.gender;
context.sendJSON({ username, gender });
}
}
The following request
curl -X POST -F 'username=Shaka Zulu' -F 'gender=M' http://domain.tld/user/register
will return
{ username: 'Shaka Zulu', gender: 'M' }
Request Content Type | Default parser |
---|---|
urlencoded | Busboy |
multipart | Busboy |
Text | None. Context.body is a string |
JSON | JSON.parse |
All others | None. Context.body is a Buffer |
The body parser can be changed by setting a custom bodyParser
configuration property.
This function should accept a request Context
object and return a promise that resolves to an object
containing two properties: body
and files
.
The files
property should represent the uploaded files is any.
E.g.
import formidable from 'formidable';
{
...,
bodyParser: myParser,
...
}
function myParser(context) {
return new Promise((resolve, reject) => {
var form = new formidable.IncomingForm();
form.parse(context.request, function(err, fields, files) {
if (err) {
return reject(err);
}
return resolve({ body: fields, files });
});
}
}
When the bodyParser
configuration property is set to false
the body is not parsed at all and the body
property is not set.
You also can also assign a custom parser to a specific controller or action only using the @bodyParser
decorator.
import { controller, post, bodyParser } from 'kikwit';
@controller
export class User {
@bodyParser(myParser)
@post
register(context) {
...
}
}