簡體   English   中英

Sails.js正則表達式路由

[英]Sails.js regex routes

我正在構建一個簡單的sails.js項目並使用主干實現前端。

理想情況下,我想要一條路由到我的骨干應用程序所在的一個索引頁面。

'/*': {
    view: 'home/index'
}

這很棒,所以現在任何URL都會轉到主頁。 除了現在,任何資產(.js,.css,.html,.jpg)的所有路徑都不再起作用。

我可以在config.routes.js看到這條評論:

// NOTE:
// You'll still want to allow requests through to the static assets,
// so we need to set up this route to ignore URLs that have a trailing ".":
// (e.g. your javascript, CSS, and image files)
'get /*(^.*)': 'UserController.profile'

但這對我沒有任何意義。 如何忽略帶有文件擴展名的路由。

我還為我的所有CRUD url添加了'api', localhost:1337/api/controller/這樣一個正則表達式路由來排除轉發那些也是必需的。 我無法找到任何有關如何在任何地方執行此操作的信息。

我必須在這里遺漏一些基本的東西。

謝謝!

您可以使用skipAssets和skipRegex標志。 這不會踩到你的靜態資產,也不會阻止以/ api開頭的api url。 我在html5Mode中使用了sails.js和angular。

  "get *":{
    controller:"PagesController",
    action:"index",
    skipAssets: true,
    skipRegex: /^\/api\/.*$/
  }

我認為這是一個錯誤,但我找到了一個解決方法。

'get /[^.?]+?': 'UserController.profile'

或者你可以使用其他的。 只有/user/profile才會執行UserController.profile 靜態目錄仍然很好用。

'get /:action( user | profile)': 'UserController.profile'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM