diff --git a/README.md b/README.md index d328a74..0e350d1 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,17 @@ server.start({ }); ``` +or add some middleware + +```js +server.start({ + middleware: [ + someMiddleware, + ['/route', someRouteSpecificMiddleware ] + ] +}); +``` + ## Global Install @@ -69,3 +80,6 @@ usage: pushstate-server [directory] [port] * `file` * Custom file to serve * defaults to `index.html` +* `middleware` + * array of middleware to use; each array element can be a function or an array of arguments for [`connect`'s `use` method](https://github.com/senchalabs/connect#use-middleware) + * defaults to `[]` diff --git a/index.js b/index.js index 6f7636d..fa9250d 100644 --- a/index.js +++ b/index.js @@ -20,10 +20,17 @@ exports.start = function (options, _onStarted) { let directories = options.directories || [directory] let file = options.file || FILE let host = options.host || HOST + let middleware = options.middleware || [] let onStarted = _onStarted || function () {} app.use(compression()) + // Add custom middleware + middleware.forEach(function(item) { + if (!Array.isArray(item)) item = [item] + app.use.apply(app, item) + }) + // First, check the file system directories.forEach(function(directory) { app.use(serveStatic(directory, { extensions: ['html'] })) diff --git a/test/index.js b/test/index.js index 8e70b2a..dbdc2ac 100644 --- a/test/index.js +++ b/test/index.js @@ -1,10 +1,16 @@ let path = require('path') let test = require('tape') let got = require('got') -let app = require('../index') + +let createApp = () => { + let module = '../index' + delete require.cache[require.resolve(module)] + return require(module) +} test('server with port', t => { + let app = createApp() let server server = app.start({ @@ -26,6 +32,7 @@ test('server with port', t => { test('server with multiple directories', t => { + let app = createApp() let server server = app.start({ @@ -49,10 +56,12 @@ test('server with multiple directories', t => { test('server with a custom host', t => { + let app = createApp() let server const host = '0.0.0.0'; server = app.start({ + directory: path.join(__dirname, 'fixtures'), host: host }, err => { @@ -68,8 +77,37 @@ test('server with a custom host', t => { }) }) +test('server with middleware', t => { + + let app = createApp() + let server + + server = app.start({ + directory: path.join(__dirname, 'fixtures'), + middleware: [ + (req, res, next) => { + res.setHeader('server', 'pushstate') + next() + } + ] + }, err => { + + if (err) return server.close(() => t.end(err)) + + got('localhost:9000') + .then(res => { + + t.equal(res.statusCode, 200, 'middleware status code') + t.equal(res.headers.server, 'pushstate', 'middleware response header') + server.close(t.end.bind(t)) + }) + .catch(err => server.close(() => t.end(err))) + }) +}) + test.skip('server with custom index file', t => { + let app = createApp() let server server = app.start({ @@ -138,6 +176,7 @@ testWithServer("serves files", (t, done) => { function testWithServer (name, done) { test(name, t => { + let app = createApp() let server server = app.start({directory: path.join(__dirname, 'fixtures')}, err => {