Simple, lazy way to serve a directory of semi-static pages in express.js. Handy for building quick "static" pages inside an otherwise "dynamic" app.

Often you have a dynamic web app that also needs a bunch of mostly "static" pages. But you still want to use the same app layouts and template system to build out those pages. Take for instance an FAQ page inside your app that otherwise has a bunch of dynamic content.

It's silly to build a specifc route for that page when all you really want is a route that at 'http://yourapp.com/faq' renders the faq.jade template in your express.js app.

I find myself adding code like this to nearly every express app I write, so I figured why the heck not publish an npm module and re-use that. So, here we are.

All it needs is the folder where your "static" templates live and the file extension your templates use. If you just need to render Jade (the default extension), you can simply call semiStatic("path/to/your/jade/templates").

If you need more customization, you can set the options as follows:

// This is with the default options

app.get("/hello*",semiStatic({

// Path to your templates

folderPath:path.dirname(require.main.filename)+"/views/static",

// Extension for your templates.

fileExt:"ejs",

// URL root for your templates.

root:"/",

// Whether to pass the request as `req` to res.render()

passReq:false,

// A custom context to send to the file, either a value or a function that

// accepts two arguments: the request and a Node-style callback that takes

// an error and return value.

context:undefined

}));

That's it, easy-peasy.

You may also include an option called context that will be passed to the rendering engine. Context can either be an object that will be passed as variables or a function that takes the req from express and a callback (whose result will then be passed to the template). If you specify passReq: true the express request object will also be passed (useful to access req.params, req.query, etc from your template).
See the example code for more details.