首页 文章

Sails.js - 通过快速中间件提供航行路线

提问于
浏览
0

我有一个风帆应用程序和路由,与应用程序关联的静态资产从root提供,它工作正常 . 我想添加一个快速中间件,以便我可以在特定路径中提供路由和静态资产 .

为了在config / http.js中的customMiddleware函数中使用下面使用的静态资源,

app.use('/my/new/path', express.static(path.join(__dirname, '../client', 'dist')));

有了上述原因,我可以从root用户和/ my / new / path加载静态文件 .

现在谈到路由我不知道如何使用app.use处理通过快速中间件加载的sails路由,例如home route在我的config / routes-client.js中默认为'/',而不是更改那里的路线我想使用下面的东西,我们通常用于典型的节点/快递应用程序,

app.use('/my/new/path', routes); --> where routes is my server routes

有没有办法在特定路径上添加快速中间件来提供sails路由?

2 回答

  • 0

    我就是这样做的......

    在http.js里面

    var express = require('express');
    
        module.exports.http = {
            customMiddleware: function (app) {
                app.use('/docs', express.static(path.join(sails.config.appPath, '/docs')));
            },
    
        ... rest of the http.js code...
        }
    

    当然在这个例子中我在路由/ docs上从我的应用程序的根目录提供docs文件夹...你加入你的逻辑......

  • 0

    我希望自动添加一个前缀到您的自定义路由,而不是直接将前缀添加到 config/routes.js (或 config-routes-client.js )文件中的路由,但这里是:

    // config/http.js
    middleware: {
      reroute: function (req, res, next) {
        // Set the prefix you want for routes.
        var prefix = '/foo/bar';
        // Create a regex that looks for the prefix in the requested URL.
        var regex = new RegExp('^' + prefix + '(/|$)');
        // If the prefix is found, replace it with /
        if (req.url.match(regex)) {
          req.url = req.url.replace(regex, '/');
        }
        // Continue processing the request.
        return next();
      }
    }
    

    确保将 reroute 添加到 config/http.js 中的 middleware.order 数组中 . 顺便说一下,这也将处理静态文件 .

相关问题