Search code examples
node.jsexpressrouteskeystonejs

How to serve a static site within keystone?


I have one keystone based site and a completely static one.

I would like to integrate the static one into the first one. All the requests to "/" will serve the static one but requests under "/resources" would serve the keystone site.

basically:

"/"            would serve the static folder 'site'
"/resources"   would serve the keystone app

At the moment my structure is:

public
| - keystone
| - site

And my keystone static option is set on 'public'

    'static': 'public'

How would I go to set up these routes?

I was thinking of using a middleware in this way:

app.use("/", express.static(__dirname + "/site"));
app.use("/resources", express.static(__dirname + "/keystone"));

But how would I do it in keystone?


Solution

  • 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 '/'