2017-03-22 61 views
0

我有一個帆應用程序,路線,與應用程序相關的靜態資產從根服務,它工作正常。我想添加一個快速中間件,這樣我就可以在特定路徑中提供路由和靜態資源。Sails.js - 通過快遞中間件服務航線路線

爲了服務我使用的下面配置/ http.js的customMiddleware函數內的靜資產,

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

有了上述我能夠從根加載靜態文件都以及來自/ my/new /路徑。

現在說到路由,我不知道如何處理通過使用app.use的快速中間件加載的風帆路由,例如home路由在我的config/routes-client.js中默認爲'/' ,而不是改變那裏的路線我想類似下面,我們通常使用一個典型的節點/快遞應用程序使用的東西,

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

有沒有辦法添加一個明確的中間件來服務於特定帆路線路徑?

+0

檢查[在Sails.js中使用快速中間件來提供靜態文件](http://stackoverflow.com/a/41604077/3058802) – gnuns

回答

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... 
    } 

當然,在這個例子中IM在路徑/文件服務docs文件夾從我的應用程序的根......你適應你的邏輯...

+0

OP已經覆蓋了靜態路由;他們具體詢問如何爲_non_-static路由添加路徑前綴(即Sails操作)。 – sgress454

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(); 
    } 
} 

確保在config/http.js添加reroutemiddleware.order陣列。順便提一下,這也會處理靜態文件。