1-
2- # Koa Body Parsers
1+ # [ ** @koa/body-parsers ** ] ( https://github.com/koajs/body-parsers )
32
43[ ![ NPM version] [ npm-image ]] [ npm-url ]
54
@@ -14,21 +13,22 @@ Includes a `json` and `urlencoded` parsers.
1413Initialization:
1514
1615``` js
17- import koaBodyParsers from ' koa-body-parsers'
18- import Koa from ' koa'
16+ // import withBodyParsers from 'koa-body-parsers'
17+ import { withBodyParsers } from " koa-body-parsers" ;
18+ import Koa from " koa" ;
1919
20- const app = new Koa ()
21- koaBodyParsers (app)
20+ const app = new Koa ();
21+ withBodyParsers (app);
2222
2323// example usage
2424app .use (async (ctx ) => {
25- const currentUser = UserService .getCurrentUser (ctx)
26- ctx .assert (currentUser, 401 )
25+ const currentUser = UserService .getCurrentUser (ctx);
26+ ctx .assert (currentUser, 401 );
2727
28- ctx .assert (ctx .request .is (' json' ), 415 )
29- const body = await ctx .request .json (' 100kb' )
30- ctx .body = body
31- })
28+ ctx .assert (ctx .request .is (" json" ), 415 );
29+ const body = await ctx .request .json (" 100kb" );
30+ ctx .body = body;
31+ });
3232```
3333
3434Because this module is a plugin for the ` context ` , the API signature is different.
@@ -41,8 +41,8 @@ Otherwise, create your server like this:
4141``` js
4242const fn = app .callback ();
4343const server = http .createServer (); // or whatever server you use
44- server .on (' request' , fn); // regular requests
45- server .on (' checkContinue' , function (req , res ) {
44+ server .on (" request" , fn); // regular requests
45+ server .on (" checkContinue" , function (req , res ) {
4646 // tag requests with `Expect: 100-continue`
4747 req .checkContinue = true ;
4848 fn (req, res);
@@ -57,11 +57,11 @@ but you would still have to call it if you're doing something like:
5757
5858``` js
5959app .use (async (ctx ) => {
60- if (ctx .request .is (' image/*' )) {
60+ if (ctx .request .is (" image/*" )) {
6161 ctx .response .writeContinue ();
62- const buffer = await ctx .request .buffer ()
62+ const buffer = await ctx .request .buffer ();
6363 }
64- })
64+ });
6565```
6666
6767### const body = await ctx.request.json([ limit] )
0 commit comments