Coder Social home page Coder Social logo

Separate routes into files? about trie-router HOT 4 CLOSED

koajs avatar koajs commented on May 21, 2024
Separate routes into files?

from trie-router.

Comments (4)

nervgh avatar nervgh commented on May 21, 2024

try return next()

const Router = require('koa-trie-router')

const router = new Router()

// middleware that is specific to this router
router.use(async (ctx, next) => {
  console.log('Time: ', Date.now())
  return next() // <-- here
})

Also you can use koa-architect that makes same things.

from trie-router.

lautiamkok avatar lautiamkok commented on May 21, 2024

@nervgh still the same after adding that.

from trie-router.

nervgh avatar nervgh commented on May 21, 2024

I understand that. You have to use one router per namespace (or use koa-mount to separation of paths):

app.js

const Koa = require('koa')
const middleware = require('./routes/index')

const app = new Koa()

app.use(middleware) // get & post
app.listen(3000)

routes/index.js

const Router = require('koa-trie-router')

const router = new Router()

// middleware that is specific to this router
router.use(async (ctx, next) => {
  console.log('Time: ', Date.now())
  await next()
})

// define the home page route
router.get('/', async (ctx, next) => {
  ctx.type = 'json'
  ctx.body = {
    message: 'Birds home page'
  }
})

// Separate this post route in a new file.
router.post('/', async (ctx, next) => {
  ctx.type = 'json'
  ctx.body = {
    message: 'Post birds home page'
  }
})

module.exports = router.middleware()

Or you could just export functions if you want to store get & post handlers in the separate files:
app.js

const Koa = require('koa')
const middleware = require('./routes/index')

const app = new Koa()

app.use(middleware) // get & post (one router)
app.listen(3000)

routes/index.js

const Router = require('koa-trie-router')
const getMiddleware = require('./routes/middleware/get')
const postMiddleware = require('./routes/middleware/get')
const router = new Router()

// define the home page route
router.get('/', getMiddleware)

// Separate this post route in a new file.
router.post('/', postMiddleware)

module.exports = router.middleware()

routes/middleware/get.js

module.exports = async (ctx, next) => {
  ctx.type = 'json'
  ctx.body = {
    message: 'Birds home page'
  }
}

routes/middleware/post.js

module.exports = async (ctx, next) => {
  ctx.type = 'json'
  ctx.body = {
    message: 'Birds home page'
  }
}

from trie-router.

nervgh avatar nervgh commented on May 21, 2024

@lautiamkok please, close this issue when it is solved.

from trie-router.

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.