Node.js 如何在keystone中为静态站点提供服务?

Node.js 如何在keystone中为静态站点提供服务?,node.js,express,routes,keystonejs,Node.js,Express,Routes,Keystonejs,我有一个基于keystone的站点和一个完全静态的站点 我想把静态的集成到第一个中。 对“/”的所有请求都将服务于静态请求,但“/resources”下的请求将服务于keystone站点 基本上: "/" would serve the static folder 'site' "/resources" would serve the keystone app 目前,我的结构是: public | - keystone | - site 我的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"));
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中我该怎么做呢?

您不需要任何额外的中间件。 使用选项
“static”:“public”
,项目文件夹中public文件夹中的任何静态内容都将作为静态内容

还可以通过提供一个目录数组来定义多个目录,如下面的
“static”:['public','site']
,这样目录站点中的所有内容都将得到服务

我假设您希望在有人点击您的站点根目录时加载某种index.html。您可以通过向
/routes/index.js
添加重定向来实现tha。
只需添加
keystone.redirect('/','/index.html')
或任何您希望获得服务的文件名。您还必须删除到
'/'

的keystone路由。我只需编辑
路由/index.js
文件中的一行,并在
keystone.init()中添加目录名即可实现这一点。现在,“客户端”目录中的所有文件都在我的网站URL的根目录下提供,Keystone位于
www.yoursite.com/resources

第一步 编辑
routes/index.js

更改:

app.get('/', routes.views.index);
致:

步骤2 编辑keystone.js

改变

keystone.init({
    ...
    'static': 'public',
    ...

```

步骤3 将站点文件(包括
index.html
)添加到名为“客户端”的新文件夹中


请记住,节点还将加载“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,我使用以下方式:

app.use("/", express.static(__dirname + "/site"));
app.use("/resources", express.static(__dirname + "/keystone"));
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,
    })
  ]}