[英]Node 8 + Typescript + Koa + koa-Router throws “TypeError: ctx.onerror is not a function”
I have a very simple server to play around with: 我有一个非常简单的服务器可以玩:
import * as http from 'http';
import * as Koa from "koa";
import { Request, Response, Context } from "koa";
import * as Router from "koa-router";
import * as bodyParser from "koa-bodyparser";
const HTTPPORT = 3000;
var app:Koa = new Koa();
var router:Router = new Router();
app.use(async (ctx:Context, next)=> {
console.log(ctx);
return await next();
});
router.get('/', function (ctx) {
ctx.body = "hello world";
console.log("success")
});
app
.use(bodyParser)
.use(router.routes())
.use(router.allowedMethods());
const httpServer = http.createServer(app.callback());
//listen on provided port
httpServer.listen(HTTPPORT, () => {
console.log(`${httpServer.address().address} (${httpServer.address().family}) is listening on port ${httpServer.address().port}`)
});
unfortunately, it throws TypeError: ctx.onerror is not a function
all the time. 不幸的是,它抛出TypeError: ctx.onerror is not a function
。
Following the examples on GitHub for koa-router ( https://github.com/alexmingoia/koa-router ), my code should work just fine... Can you help me to sort out what is the problem with this onError message? 按照GitHub上koa-router的例子( https://github.com/alexmingoia/koa-router ),我的代码应该可以正常工作......你能帮我解决一下这个onError消息的问题吗?
starting the server logs the following: 启动服务器记录以下内容:
:: (IPv6) is listening on port 3000
{ request:
{ method: 'GET',
url: '/',
header:
{ host: 'localhost:3000',
connection: 'keep-alive',
'cache-control': 'max-age=0',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'upgrade-insecure-requests': '1',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
dnt: '1',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9' } },
response: { status: 404, message: 'Not Found', header: {} },
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>' }
/.../node_modules/koa/lib/application.js:147
const onerror = err => ctx.onerror(err);
^
TypeError: ctx.onerror is not a function
at Array.onerror (/.../node_modules/koa/lib/application.js:147:32)
at listener (/.../node_modules/on-finished/index.js:169:15)
at onFinish (/.../node_modules/on-finished/index.js:100:5)
at callback (/.../node_modules/ee-first/index.js:55:10)
at ServerResponse.onevent (/...s/node_modules/ee-first/index.js:93:5)
at emitNone (events.js:111:20)
at ServerResponse.emit (events.js:208:7)
at onFinish (_http_outgoing.js:723:10)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Note After finding the solution I uploaded the working code to gethub for everyone interested, please find it here: https://github.com/wzr1337/node.koa.webpack.starter 注意找到解决方案后,我将工作代码上传到gethub,感兴趣的人,请在此处找到: https : //github.com/wzr1337/node.koa.webpack.starter
bummer... it was bodyParser
vs. bodyParser()
bodyParser
...它是bodyParser
vs. bodyParser()
so 所以
app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());
work perfectly fine.. 工作完全正常..
Thanks @saadq for the hint.. I was to blind to see I was missing the ()
... 谢谢@saadq提示..我是盲目地看到我错过了()
......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.