【问题标题】:How to serve a static site within keystone?如何在 keystone 中提供静态站点?
【发布时间】:2016-01-24 14:54:35
【问题描述】:

我有一个基于 keystone 的站点和一个完全静态的站点。

我想将静态的集成到第一个中。 所有对“/”的请求都将为静态请求提供服务,但“/resources”下的请求将为基石站点提供服务。

基本上:

"/"            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 中我该怎么做呢?

【问题讨论】:

    标签: node.js express routes keystonejs


    【解决方案1】:

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

    还可以定义多个目录,方法是提供像'static' : ['public','site'] 这样的目录数组,这样目录站点中的所有内容也将被提供。

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

    【讨论】:

      【解决方案2】:

      我通过在routes/index.js 文件中编辑一行并在keystone.init()“静态”设置中添加目录名称来实现这一点。现在“client”目录中的所有文件都在我的站点 URL 的根目录下提供,Keystone 位于 www.yoursite.com/resources

      步骤 1

      编辑routes/index.js:

      变化:

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

      收件人:

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

      第二步

      编辑keystone.js:

      改变

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

      keystone.init({
          ...
          'static': ['public','client']
          ...
      

      ```

      第三步

      将站点文件(包括index.html)添加到名为“client”的新文件夹中。

      请记住,Node 也会加载“公共”文件夹中的文件。例如,目前 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'],则“客户端”中的图像优先

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-03-08
          • 2013-09-08
          • 2019-12-29
          • 2021-07-30
          • 2015-10-17
          • 2014-04-12
          • 2019-04-05
          相关资源
          最近更新 更多