【问题标题】:How to assign routes within a base route in fastify如何在 fastify 中分配基本路由中的路由
【发布时间】:2019-12-26 14:44:30
【问题描述】:

我在我的 nodejs 项目中使用 fastify 作为 web 框架。我想从一个目录中调用我的所有路由,该目录在主 JS 文件中定义了一个基本路由,就像我们在 express 中所做的那样。我已经阅读了很多博客,但我没有找到任何与我的问题相关的答案

就像在 express 中一样,我们将路线分配为-

app.use('/user', user_route)

然后在 user_route 我们定义所有其他路由方法。

我在 fastify 中使用过

fastify.register(require('./routes/users'), { prefix: '/user' })

但是只有一个函数可以被调用——

module.exports = function (fastify, opts, done) {
  fastify.get('/user', handler_v1)
  done()
}

如果我想调用多路由函数怎么办?

【问题讨论】:

  • 为什么你认为只能调用一个函数?您是否尝试过添加多个fastify.get(...)fastify.post(..)
  • 谢谢它解决了我的问题。但它是声明路由的正确方式吗?
  • 可能还有其他方法可以做到这一点,但我认为这种方法没有任何问题。

标签: javascript node.js express routes fastify


【解决方案1】:

Fastify 支持这种更有条理的方式。我一步一步解释。

  1. 创建一个目录名称,例如 routers
  2. 此目录可能包含一个或多个 .js 文件。每个文件包含一个或多个路由。喜欢 -
async function routes(fastify, options){
    fastify.get('/', async function(request, reply) {
         return {hello: 'world'} 
    }), 

    fastify.get('/bye', async function(request, reply) {
         return {bye: 'good bye'} 
    }) 
}

module.exports = routes

  1. 接下来使用 fastify-autoload 注册此目录。喜欢
const path = require('path')
const autoload = require('fastify-autoload')

async function app(fastify, options){
    //old standard routing
    //fastify.register(require('./routes/baisc-router'))

    //auto-load, based on directory 
    fastify.register(autoload,{
         dir: path.join(__dirname, 'routes')
    })
}
module.exports = app

详细代码可以关注这个Git repository

【讨论】:

    【解决方案2】:

    要使基本路由在所有路由中全局工作,您可以在 server.js 或 app.js 中注册它,无论您使用什么来注册您的服务器。

     fastify.register(require('../app/routes'), { prefix: 'api/v1' });
    

    这里的 '../app/routes' 指向你的路由目录。您定义的所有路由都将以 'api/v1' 为前缀

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      你可以像这样向一个 fastify 实例添加许多路由:

      'use strict'
      
      const Fastify = require('fastify')
      const fastify = Fastify({ logger: true })
      
      fastify.register((instance, opts, next) => {
      
        instance.get('/', (req, res) => { res.send(req.raw.method) })
        instance.post('/', (req, res) => { res.send(req.raw.method) })
        instance.put('/', (req, res) => { res.send(req.raw.method) })
        instance.patch('/', (req, res) => { res.send(req.raw.method) })
      
        instance.get('/other', (req, res) => { res.send('other code') })
      
        next()
      }, { prefix: 'user' })
      
      
      fastify.listen(3000, () => {
        console.log(fastify.printRoutes());
      })
      

      .register 方法仅用于封装上下文和插件。 这有助于将您的代码库拆分为较小的文件并仅加载您需要的插件。

      通过这种方式,您将拥有一个对不同 HTTP 方法做出不同响应的路由:curl -X GET http://localhost:3000/user/curl -X PUT http://localhost:3000/user/

      【讨论】:

      • 这对我帮助很大。谢谢!!
      【解决方案4】:

      你可以使用 fastify-autoload 插件

      const AutoLoad = require('fastify-autoload')
      // define your routes in one of these
      fastify.register(AutoLoad, {
          dir: path.join(__dirname, 'services'),
          options: Object.assign({ prefix: '/api' }, opts)
      

      })

      【讨论】:

        猜你喜欢
        • 2020-03-27
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        • 2023-02-02
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        相关资源
        最近更新 更多