1 |
|
2 | var Router, Turnout, path, turnoutMiddleware;
|
3 |
|
4 | Router = (require('express')).Router;
|
5 |
|
6 | Turnout = require('./turnout');
|
7 |
|
8 | path = require('path');
|
9 |
|
10 | turnoutMiddleware = function(options) {
|
11 | var router, turnout;
|
12 | if (options == null) {
|
13 | options = {};
|
14 | }
|
15 | turnout = new Turnout(options);
|
16 | router = Router();
|
17 | router.get('/express-turnout.js', function(req, res) {
|
18 | return res.sendFile(path.resolve(__dirname, '..', 'lib', 'express-turnout.js'));
|
19 | });
|
20 | router.use(function(req, res, next) {
|
21 | if (!turnout.isBot(req)) {
|
22 | return next();
|
23 | }
|
24 | res.setHeader('x-powered-by', 'Express-turnout');
|
25 | return turnout.render(req).then(function(html) {
|
26 | res.status(200);
|
27 | res.set('Content-Type', 'text/html');
|
28 | return res.end(html);
|
29 | })["catch"](Error, function(error) {
|
30 | res.status(500);
|
31 | return res.end(error.message);
|
32 | })["catch"](function(error) {
|
33 | res.status(403);
|
34 | return res.end(error);
|
35 | });
|
36 | });
|
37 | return router;
|
38 | };
|
39 |
|
40 | module.exports = turnoutMiddleware;
|