【发布时间】:2016-11-07 10:16:59
【问题描述】:
我想在我的sails js 应用程序中完全分离客户端和服务器端。
如果我删除“/”路由,它会自动从“资产”文件夹中提供一个名为 index.html 的文件。 我想从 assets 文件夹中提供另一个文件,我该怎么做?
【问题讨论】:
标签: static sails.js single-page-application assets
我想在我的sails js 应用程序中完全分离客户端和服务器端。
如果我删除“/”路由,它会自动从“资产”文件夹中提供一个名为 index.html 的文件。 我想从 assets 文件夹中提供另一个文件,我该怎么做?
【问题讨论】:
标签: static sails.js single-page-application assets
如果你看一下sails 使用的中间件
config/http.js
order: [
'startRequestTimer',
'cookieParser',
'session',
'myRequestLogger',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
],
我们可以看到它尝试将当前请求与config/routes.js 中定义的router 中间件匹配,如果没有找到,则尝试在www 中间件中提供静态文件。如果两者都失败,则返回 404。
“www”中间件
www 中间件只是使用 express 的 serve-static 中间件。
www: (function() {
var flatFileMiddleware = require('serve-static')(sails.config.paths['public'], {
maxAge: sails.config.http.cache
});
return flatFileMiddleware;
})(),
默认情况下,此模块将发送“index.html”文件以响应 请求目录。
所以如果你想使用sai的默认中间件,那么你可以将你的html文件放入assets/index.html、assets/about/index.html和assets/foo/index.html,它们将被提供给/、/about和/foo分别。
但是,如果你真的想控制这个,那么你可以替换 www 中间件并用你自己的中间件替换它。在middlewares 和serve-static's documentation 上阅读sai 的文档。
您可以查看有关此主题的类似问题:Any way to serve static html files from express without the extension?
【讨论】:
您可以将 Html 文件放在 assets 文件夹中。 Sails 将为请求的 url 提供 html 文件。
假设您将一个html文件about.html放在assets文件夹中,当请求一个url <your-domain>/about.html时,Sails会提供它。
【讨论】:
assets/about/index.html