express-turnout
Version:
Pre-rendering Single-Page-Application for crawlers.
41 lines (35 loc) • 1.05 kB
JavaScript
// Generated by CoffeeScript 1.9.3
var Router, Turnout, path, turnoutMiddleware;
Router = (require('express')).Router;
Turnout = require('./turnout');
path = require('path');
turnoutMiddleware = function(options) {
var router, turnout;
if (options == null) {
options = {};
}
turnout = new Turnout(options);
router = Router();
router.get('/express-turnout.js', function(req, res) {
return res.sendFile(path.resolve(__dirname, '..', 'lib', 'express-turnout.js'));
});
router.use(function(req, res, next) {
if (!turnout.isBot(req)) {
return next();
}
res.setHeader('x-powered-by', 'Express-turnout');
return turnout.render(req).then(function(html) {
res.status(200);
res.set('Content-Type', 'text/html');
return res.end(html);
})["catch"](Error, function(error) {
res.status(500);
return res.end(error.message);
})["catch"](function(error) {
res.status(403);
return res.end(error);
});
});
return router;
};
module.exports = turnoutMiddleware;