admin管理员组文章数量:1332746
I'm trying to learn koa and can't figure out why i'm getting the error:
server error TypeError: ctx.body is not a function
at getHandler (/Users/tomcaflisch/Sites/learn-koa/server.js:32:7)
when I run this code:
'use strict'
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
function server (app) {
const router = new Router()
router.get('/foo', getHandler)
app.use(bodyParser())
app.use(router.routes())
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
}
});
app.on('error', (err, ctx) => {
console.log('server error', err, ctx)
});
app.listen(4000)
}
function getHandler (ctx, next) {
// ctx.set('Location', 'http://localhost:3000/foo')
ctx.body({ foo: 'bar' })
}
module.exports = server
I'm trying to learn koa and can't figure out why i'm getting the error:
server error TypeError: ctx.body is not a function
at getHandler (/Users/tomcaflisch/Sites/learn-koa/server.js:32:7)
when I run this code:
'use strict'
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
function server (app) {
const router = new Router()
router.get('/foo', getHandler)
app.use(bodyParser())
app.use(router.routes())
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
}
});
app.on('error', (err, ctx) => {
console.log('server error', err, ctx)
});
app.listen(4000)
}
function getHandler (ctx, next) {
// ctx.set('Location', 'http://localhost:3000/foo')
ctx.body({ foo: 'bar' })
}
module.exports = server
Share
Improve this question
asked Apr 4, 2019 at 4:29
CatfishCatfish
19.3k60 gold badges213 silver badges358 bronze badges
3 Answers
Reset to default 5 +50It's exactly what the issue says it is: ctx.body is not a function
From the docs:
A Koa Response object is an abstraction on top of node's vanilla response object
Response aliases
The following accessors and alias Response equivalents:
ctx.body
ctx.body=
So essentially ctx.body
is an object to which you assign something to be sent as response.
If you look at the Hello World
example, the response is just assigned to the Response
object which then koa
sends.
app.use(async ctx => {
ctx.body = 'Hello World';
});
So, changing your code to following serves the response body as json
function getHandler (ctx, next) {
// ctx.set('Location', 'http://localhost:3000/foo')
ctx.body = { foo: 'bar' };
}
You are aware that GET request don't have a body, only POST request do?
From koajs/bodyparser
docs
ctx.body
doesn't exist, and it's ctx.request.body
returning the JSON object (not a function)
本文标签: javascriptTrying to use koa bodyparser and ctxbody undefinedStack Overflow
版权声明:本文标题:javascript - Trying to use koa bodyparser and ctx.body undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742339896a2456386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论