如何在keystone中提供静态站点?

时间:2015-10-25 12:59:13

标签: node.js express routes keystonejs

我有一个基于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中做到这一点?

3 个答案:

答案 0 :(得分:7)

You don't need any extra middleware. With the option 'static' : 'public', any static content placed in the public folder within your project folder will be served as static content.

It is also possible to define more than one directory by providing an array of directories like this 'static' : ['public','site'] this way everything in the directory site will also be served.

I assume you want some kind of index.html to be loaded when someone hits your sites root. You can achieve tha by adding a redirect to your /routes/index.js. Simply add keystone.redirect('/', '/index.html'); or whatever the filename is you want to get served. You also have to remove the keystone route to '/'

答案 1 :(得分:3)

我通过编辑routes/index.js文件中的一行,并在keystone.init()“静态”设置中添加目录名来实现此目的。现在,“client”目录中的所有文件都在我的网站网址的根目录下提供,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.gifclient/images/logo-email.gif,则public中的图像优先。如果您反转keystone.init()中的“静态”属性,使其显示为['client','public'],则“客户端”中的图像优先

答案 2 :(得分:0)

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,
    })
  ]}