Coder Social home page Coder Social logo

Comments (6)

fl0w avatar fl0w commented on May 5, 2024 2

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)

from compose.

snowdream avatar snowdream commented on May 5, 2024
HuideMacBook-Pro:kvmt snowdream$ DEBUG=* npm start

> [email protected] start /Users/snowdream/workspace/snowdream/git/kvmt
> node bin/www

  koa-static static "/Users/snowdream/workspace/snowdream/git/kvmt/public" {} +0ms
  koa-router defined route HEAD,GET / +10ms
  koa-router defined route HEAD,GET /string +1ms
  koa-router defined route HEAD,GET /json +0ms
  koa-router defined route HEAD,GET /users/ +0ms
  koa-router defined route HEAD,GET /users/bar +0ms
  koa:application use - +7ms
  koa:application use dispatch +0ms
  koa:application use dispatch +0ms
  demo:server Listening on port 3000 +8ms

from compose.

snowdream avatar snowdream commented on May 5, 2024

http://localhost:3000

Not Found

from compose.

fl0w avatar fl0w commented on May 5, 2024

Try

app.use(index.routes())
app.use(index.allowedMethods())
app.use(users.routes())
app.use(users.allowedMethods())

from compose.

snowdream avatar snowdream commented on May 5, 2024

@fl0w it does not work.

from compose.

trevorhreed avatar trevorhreed commented on May 5, 2024

Why does...

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

...not work?

from compose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.