Search code examples
javascriptnode.jsbabeljskoa2koa-router

Koa.js route() is not a function


I just made simple koa app that returns rss xml by tag using parameter. and seems like middleware can't read router from router file. I have no idea why It doesn't work. I'm running this app.js with babel-node. and It keeps saying this error below

app.use((0, _koaLogger2.default)()).use((0, _routes2.default)());
                                                             ^
TypeError: (0 , _routes2.default) is not a function

route/index.js

import Router from 'koa-router'
const router = new Router({ prefix: '/'})

router.get('/:tag', async (ctx, next) => 
    (ctx.body = await rssGenerator(this.param.tag)))

export default router

app.js

import Koa from 'koa'
import logger from 'koa-logger'

import routes from './routes'

const app = new Koa()
const port = process.env.PORT || 3000

app
  .use(logger())
  .use(routes())

app.listen(port, () => console.log("[!] Server STARTED"))

Solution

  • I see 2 problems here in your code:

    First: you are importing routes like this:

    import routes from './routes'
    

    but in your code above the path is route/index.js and not routes

    Second: in route/index.js you are exporting router

    export default router
    

    but then you are trying to import routes