gpt4 book ai didi

javascript - Node 8 + Typescript + Koa + koa-Router 抛出 "TypeError: ctx.onerror is not a function"

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:47 26 4
gpt4 key购买 nike

我有一个非常简单的服务器可以使用:

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}`)
});

不幸的是,它一直抛出 TypeError: ctx.onerror is not a function

按照 GitHub 上的 koa-router ( https://github.com/alexmingoia/koa-router ) 示例,我的代码应该可以正常工作......你能帮我弄清楚这个 onError 消息有什么问题吗?

启动服务器会记录以下内容:

:: (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)

注意 找到解决方案后,我将工作代码上传到 gethub 供所有感兴趣的人使用,请在此处找到:https://github.com/wzr1337/node.koa.webpack.starter

最佳答案

真可惜...它是 bodyParserbodyParser()

所以

app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());

工作得很好..

感谢@saadq 的提示。我是瞎了眼才发现我错过了 ()...

关于javascript - Node 8 + Typescript + Koa + koa-Router 抛出 "TypeError: ctx.onerror is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48203011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com