Search code examples
node.jsgruntjsyeomanssinode.js-connect

Server side includes (SSI) with grunt connect web server


We are using yeoman for our dev process and currently using the "grunt server" command to run the grunt connect web server for local development. Every time we save a file, grunt will run all its tasks and reload the browser.

The problem is with Server side includes we use to include the header and footer. We had it previously working with Apache, IIS and Tomcat but have no idea how to get connect to do the same. It just treats it as an html comment.

eg include:

<!--#include virtual="header.html" --> 

So, 1. Is there a way to get grunt/connect to include these files? 2. If not can we use Apache with yeoman/grunt? 3. If all fails, is there another way to include files with connect?


Solution

  • You can have express handle SSI with the help of the ssi node module.

    I put together a github repo with this simple example: https://github.com/sfarthin/express-ssi-example .

    I deployed this app to heroku so you can see it in action: http://intense-basin-9464.herokuapp.com/

    app.use(function(req,res,next) {
        var filename    = __dirname+(req.path == "/" ? "/index.shtml" : req.path);
    
        if(fs.existsSync(filename)) {
            res.send(parser.parse(filename, fs.readFileSync(filename, {encoding: "utf8"})).contents);   
        } else {
            next();
        }
    });