Logo Questions Linux Laravel Mysql Ubuntu Git Menu

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)=> {
  return await next();

router.get('/', function (ctx) {
  ctx.body = "hello world";


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.

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?

starting the server logs the following:

:: (IPv6) is listening on port 3000
{ request:
   { method: 'GET',
     url: '/',
      { 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>' }
    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

like image 741
wzr1337 Avatar asked Jan 11 '18 08:01


1 Answers

bummer... it was bodyParser vs. bodyParser()



work perfectly fine..

Thanks @saadq for the hint.. I was to blind to see I was missing the ()...

like image 121
wzr1337 Avatar answered Nov 07 '22 08:11