Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to add custom middleware #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ server.start({
});
```

or add some middleware

```js
server.start({
middleware: [
someMiddleware,
['/route', someRouteSpecificMiddleware ]
]
});
```


## Global Install

Expand Down Expand Up @@ -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 `[]`
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }))
Expand Down
41 changes: 40 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -26,6 +32,7 @@ test('server with port', t => {

test('server with multiple directories', t => {

let app = createApp()
let server

server = app.start({
Expand All @@ -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 => {

Expand All @@ -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({
Expand Down Expand Up @@ -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 => {
Expand Down