如何在 keystone 中提供静态站点?
How to serve a static site within keystone?
我有一个基于 keystone 的站点和一个完全静态的站点。
我想将静态的整合到第一个中。
所有对“/”的请求都将服务于静态请求,但“/resources”下的请求将服务于 keystone 站点。
基本上:
"/" would serve the static folder 'site'
"/resources" would serve the keystone app
目前我的结构是:
public
| - keystone
| - site
我的 keystone 静态选项设置在 'public'
'static': 'public'
我将如何设置这些路由?
我正在考虑这样使用中间件:
app.use("/", express.static(__dirname + "/site"));
app.use("/resources", express.static(__dirname + "/keystone"));
但是在 keystone 中我该怎么做呢?
您不需要任何额外的中间件。
使用选项 'static' : 'public'
,项目文件夹中 public 文件夹中的任何静态内容都将作为静态内容提供。
也可以通过提供一组这样的目录来定义多个目录 'static' : ['public','site']
这样目录站点中的所有内容也将被提供。
我假设您希望在有人访问您的网站根目录时加载某种 index.html。您可以通过向 /routes/index.js
添加重定向来实现。
只需添加 keystone.redirect('/', '/index.html');
或您想要提供的任何文件名。您还必须删除到 '/'
的 keystone 路由
我通过在 routes/index.js
文件中编辑一行,并在 keystone.init()
"static" 设置中添加目录名称来实现这一点。现在 "client" 目录中的所有文件都在我的站点 URL 的根目录下提供,Keystone 位于 www.yoursite.com/resources
第 1 步
编辑routes/index.js
:
变化:
app.get('/', routes.views.index);
收件人:
app.get('/resources', routes.views.index);
第 2 步
编辑keystone.js
:
改变
keystone.init({
...
'static': 'public',
...
到
keystone.init({
...
'static': ['public','client']
...
```
第 3 步
将站点文件(包括 index.html
)添加到名为 "client" 的新文件夹中。
请记住,Node 还将加载 "public" 文件夹中的文件。例如,目前keystone在public/images
中有一个名为"logo-email.gif"的文件。这意味着 www.yoursite.com/images/logo-email.gif
将加载该图像。事实证明,如果同时存在文件 public/images/logo-email.gif
和 client/images/logo-email.gif
,则 public
中的图像优先。如果您反转 keystone.init()
中的 'static' 属性,使其显示 ['client','public']
,则 "client" 中的图像优先
KeystoneJS v5,我是这样用的:
module.exports = {
keystone,
apps: [
new GraphQLApp(),
new StaticApp({path:"/", src:'public'}), // for your static site
new StaticApp({path:"/resources", src:'static'}), // Keystone uploaded resources
new AdminUIApp({
enableDefaultRoute: true,
authStrategy,
})
]}
我有一个基于 keystone 的站点和一个完全静态的站点。
我想将静态的整合到第一个中。 所有对“/”的请求都将服务于静态请求,但“/resources”下的请求将服务于 keystone 站点。
基本上:
"/" would serve the static folder 'site'
"/resources" would serve the keystone app
目前我的结构是:
public
| - keystone
| - site
我的 keystone 静态选项设置在 'public'
'static': 'public'
我将如何设置这些路由?
我正在考虑这样使用中间件:
app.use("/", express.static(__dirname + "/site"));
app.use("/resources", express.static(__dirname + "/keystone"));
但是在 keystone 中我该怎么做呢?
您不需要任何额外的中间件。
使用选项 'static' : 'public'
,项目文件夹中 public 文件夹中的任何静态内容都将作为静态内容提供。
也可以通过提供一组这样的目录来定义多个目录 'static' : ['public','site']
这样目录站点中的所有内容也将被提供。
我假设您希望在有人访问您的网站根目录时加载某种 index.html。您可以通过向 /routes/index.js
添加重定向来实现。
只需添加 keystone.redirect('/', '/index.html');
或您想要提供的任何文件名。您还必须删除到 '/'
我通过在 routes/index.js
文件中编辑一行,并在 keystone.init()
"static" 设置中添加目录名称来实现这一点。现在 "client" 目录中的所有文件都在我的站点 URL 的根目录下提供,Keystone 位于 www.yoursite.com/resources
第 1 步
编辑routes/index.js
:
变化:
app.get('/', routes.views.index);
收件人:
app.get('/resources', routes.views.index);
第 2 步
编辑keystone.js
:
改变
keystone.init({
...
'static': 'public',
...
到
keystone.init({
...
'static': ['public','client']
...
```
第 3 步
将站点文件(包括 index.html
)添加到名为 "client" 的新文件夹中。
请记住,Node 还将加载 "public" 文件夹中的文件。例如,目前keystone在public/images
中有一个名为"logo-email.gif"的文件。这意味着 www.yoursite.com/images/logo-email.gif
将加载该图像。事实证明,如果同时存在文件 public/images/logo-email.gif
和 client/images/logo-email.gif
,则 public
中的图像优先。如果您反转 keystone.init()
中的 'static' 属性,使其显示 ['client','public']
,则 "client" 中的图像优先
KeystoneJS v5,我是这样用的:
module.exports = {
keystone,
apps: [
new GraphQLApp(),
new StaticApp({path:"/", src:'public'}), // for your static site
new StaticApp({path:"/resources", src:'static'}), // Keystone uploaded resources
new AdminUIApp({
enableDefaultRoute: true,
authStrategy,
})
]}