From 498bc1fb44c78433bdef35224c95dc863856cc9a Mon Sep 17 00:00:00 2001 From: analog-nico Date: Mon, 22 Dec 2014 22:13:35 -0500 Subject: [PATCH] Allowing preproc.js to export an express.Router() --- README.md | 4 +++- lib/index.js | 2 +- package.json | 2 +- .../serve/preproc-with-router/index.htmlt | 1 + .../serve/preproc-with-router/preproc.js | 19 +++++++++++++++++++ test/spec/init.js | 3 +++ test/spec/render.js | 9 +++++++++ 7 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/serve/preproc-with-router/index.htmlt create mode 100644 test/fixtures/serve/preproc-with-router/preproc.js diff --git a/README.md b/README.md index 46ae905..cd42bd0 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,10 @@ If you want to debug a test you should use `gulp test-without-coverage` to run a ## Change History +- v0.1.1 (2014-12-22) + - Allowing preproc.js to export an `express.Router()` - v0.1.0 (2014-12-06) - - Initial version + - Initial version ## License (ISC) diff --git a/lib/index.js b/lib/index.js index 90c4b43..86b4158 100644 --- a/lib/index.js +++ b/lib/index.js @@ -119,7 +119,7 @@ module.exports = function initServeSpa(app, root, options) { function render() { - if (arguments.length > 0) { + if ((arguments.length === 1 && arguments[0] !== null) || arguments.length > 1) { // Error provided or route forward intended... return next.apply(undefined, arguments); } diff --git a/package.json b/package.json index eff68e2..7dcba00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serve-spa", - "version": "0.1.0", + "version": "0.1.1", "description": "Express middleware to serve single page applications in a performant and SEO friendly way", "keywords": [ "express", diff --git a/test/fixtures/serve/preproc-with-router/index.htmlt b/test/fixtures/serve/preproc-with-router/index.htmlt new file mode 100644 index 0000000..d17f7c5 --- /dev/null +++ b/test/fixtures/serve/preproc-with-router/index.htmlt @@ -0,0 +1 @@ +with - <%= req.url %> - <%= req.test %> \ No newline at end of file diff --git a/test/fixtures/serve/preproc-with-router/preproc.js b/test/fixtures/serve/preproc-with-router/preproc.js new file mode 100644 index 0000000..af2d088 --- /dev/null +++ b/test/fixtures/serve/preproc-with-router/preproc.js @@ -0,0 +1,19 @@ +'use strict'; + +var express = require('express'); + + +var router = express.Router(); + +// FIXME: The router should register relative paths. In this case just /edit/:_id +router.get('/preproc-with-router/edit/:_id', function (req, res, next) { + req.test = req.params._id; + next(); +}); + +router.get('/preproc-with-router/', function (req, res, next) { + req.test = 'base'; + next(); +}); + +module.exports = router; diff --git a/test/spec/init.js b/test/spec/init.js index 5ff025c..834047a 100644 --- a/test/spec/init.js +++ b/test/spec/init.js @@ -66,6 +66,9 @@ describe('Regarding its initialization, Serve-SPA', function () { '/async-preproc/index.htmlt', '/async-preproc/preproc.js', '/index.htmlt', + '/preproc-with-router', + '/preproc-with-router/index.htmlt', + '/preproc-with-router/preproc.js', '/preproc.js', '/sub1', '/sub1/index.htmlt', diff --git a/test/spec/render.js b/test/spec/render.js index f6b843b..45f2dbb 100644 --- a/test/spec/render.js +++ b/test/spec/render.js @@ -50,6 +50,15 @@ describe('Regarding rendering the template, Serve-SPA', function () { }); + it('should allow a router to be used for preprocessing', function () { + + return rp('http://localhost:4000/preproc-with-router/edit/0123456789') + .then(function (body) { + expect(body).to.equal("with - /preproc-with-router/edit/0123456789 - 0123456789"); + }); + + }); + it('should provide req, res, and require for rendering', function () { return rp('http://localhost:4000/')