question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

when i use koa-compose with koa-router, koa-router does not work.

See original GitHub issue

app.js

const Koa = require('koa');
const app = new Koa();
const views = require('koa-views')(__dirname + '/views', {
    extension: 'pug'
});
const json = require('koa-json');
const onerror = require('koa-onerror');
const bodyparser = require('koa-bodyparser')({
    enableTypes:['json', 'form', 'text']
});
const logger = require('koa-logger');
const koa_static = require('koa-static')(__dirname + '/public')
const compose = require('koa-compose');

const index = require('./routes/index');
const users = require('./routes/users');

// error handler
onerror(app);

// composed middleware
const all = compose([
    bodyparser,
    json,
    logger,
    koa_static,
    views
]);

// routes
app.use(all);

// routes
app.use(index.routes(), index.allowedMethods());
app.use(users.routes(), users.allowedMethods());

module.exports = app;

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
fl0wcommented, Jun 14, 2017

Well, looking at this more, you need to instantiate koa-json e.g.

const json = require('koa-json')()

You’re passing in constructors which are not middleware that Koa understands.

The following minimal works as expected,

'use strict'

const Koa = require('koa')
const Router = require('koa-router')
const compose = require('koa-compose')
const json = require('koa-json')
const logger = require('koa-logger')

const app = new Koa()
const router = new Router()
// Correctly return a Koa middleware for all mw
const middleware = compose([json(), logger()])

router.get('/', async ctx => {
  ctx.body = 'It should work'
})

app.use(middleware)
app.use(router.routes())
app.use(router.allowedMethods())

app.listen(1337)
0reactions
trevorhreedcommented, Feb 20, 2020

Why does…

app.use(compose([
  router.routes(),
  router.allowedMethods()
]))

…not work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

koa router doesn't work, sends 404 - Stack Overflow
1 Answer 1 ... it can help you to solve problem. maybe work. good luck! Share. Share a link to this answer.
Read more >
koa-router | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
koa-router | koajs
Route paths will be translated to regular expressions used to match requests. Query strings will not be considered when matching requests. 命名路由. Routes...
Read more >
koa-router - npm
Express-style routing using app.get , app.put , app.post , etc. Named URL parameters and regexp captures. String or regular expression route ...
Read more >
Use Array of Middleware in Koa Router - realguess
But for every router is one handler + formidable or + authed function.. Chao Huang • 7 years ago. Not every route has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found