koa2 使用带有简单 GET 的 koa-bodyparser
koa2 using koa-bodyparser with a simple GET
我正在尝试了解如何使用简单的 GET
配置 koa-bodyparser。以下 returns 完全相同的正文,如果我包含解析器或不包含:
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
const router = new Router();
app.use(bodyParser());
var users = [{
id: 1,
firstName: 'John',
lastName: 'Doe'
}, {
id: 2,
firstName: 'Jane',
lastName: 'Doe'
}];
router
.get('/api', async (ctx, next) => {
console.log('Getting users from /api');
ctx.body = ctx.request.body = users;
});
app.use(router.routes());
app.listen(3000, () => console.log('Koa app listening on 3000'));
documentation 表示:
// the parsed body will store in ctx.request.body
// if nothing was parsed, body will be an empty object {}
ctx.body = ctx.request.body;
我不确定我是否正确解析了:
ctx.body = ctx.request.body = users;
koa-bodyparser
解析 请求 body,而不是 响应 body。在 GET 请求中,您通常不会收到 body 请求。要return一个JSONbody给来电者,你需要做的就是
router
.get('/api', async ctx => {
console.log('Getting users from /api');
ctx.body = users;
});
如果您想查看解析的实际效果,您将需要 PUT、POST、PATCH 等。
router
.post('/api', async ctx => {
console.log('creating user for /api');
const user = ctx.request.body;
// creation logic goes here
// raw input can be accessed from ctx.request.rawBody
ctx.status = 201;
});
您需要在 post 请求中通过 Postman 或 curl
传递有效的 JSON,如下所示:
curl -X POST \
http://localhost:3000/api \
-H 'Content-Type: application/json' \
-d '{
"firstName": "Zaphod",
"lastName": "Beeblebrox"
}'
您会发现 ctx.request.body
有一个 JSON 值,而 ctx.request.rawBody
有一个字符串值 '{"firstName":"Zaphod","lastName":"Beeblebrox"}'
。这给你带来了什么?在这种情况下,它使您不必调用 JSON.parse(ctx.request.body)
来获得有效的 JSON。 koa-bodyparser
不仅如此,因为它在文档中有统计,它根据随请求传递的 Content-Type
header 处理 JSON、表单和文本正文。
我正在尝试了解如何使用简单的 GET
配置 koa-bodyparser。以下 returns 完全相同的正文,如果我包含解析器或不包含:
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
const router = new Router();
app.use(bodyParser());
var users = [{
id: 1,
firstName: 'John',
lastName: 'Doe'
}, {
id: 2,
firstName: 'Jane',
lastName: 'Doe'
}];
router
.get('/api', async (ctx, next) => {
console.log('Getting users from /api');
ctx.body = ctx.request.body = users;
});
app.use(router.routes());
app.listen(3000, () => console.log('Koa app listening on 3000'));
documentation 表示:
// the parsed body will store in ctx.request.body
// if nothing was parsed, body will be an empty object {}
ctx.body = ctx.request.body;
我不确定我是否正确解析了:
ctx.body = ctx.request.body = users;
koa-bodyparser
解析 请求 body,而不是 响应 body。在 GET 请求中,您通常不会收到 body 请求。要return一个JSONbody给来电者,你需要做的就是
router
.get('/api', async ctx => {
console.log('Getting users from /api');
ctx.body = users;
});
如果您想查看解析的实际效果,您将需要 PUT、POST、PATCH 等。
router
.post('/api', async ctx => {
console.log('creating user for /api');
const user = ctx.request.body;
// creation logic goes here
// raw input can be accessed from ctx.request.rawBody
ctx.status = 201;
});
您需要在 post 请求中通过 Postman 或 curl
传递有效的 JSON,如下所示:
curl -X POST \
http://localhost:3000/api \
-H 'Content-Type: application/json' \
-d '{
"firstName": "Zaphod",
"lastName": "Beeblebrox"
}'
您会发现 ctx.request.body
有一个 JSON 值,而 ctx.request.rawBody
有一个字符串值 '{"firstName":"Zaphod","lastName":"Beeblebrox"}'
。这给你带来了什么?在这种情况下,它使您不必调用 JSON.parse(ctx.request.body)
来获得有效的 JSON。 koa-bodyparser
不仅如此,因为它在文档中有统计,它根据随请求传递的 Content-Type
header 处理 JSON、表单和文本正文。