Skip to content

Commit

Permalink
Accept multiple middlewares in app.get (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva authored and lukechilds committed Feb 17, 2018
1 parent 5788767 commit 23319da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/index.js
Expand Up @@ -12,8 +12,8 @@ const createTestServer = opts => createCert(opts && opts.certificate)
const get = app.get.bind(app);
const server = http.createServer(app);
const sslServer = https.createServer(keys, app);
const send = fn => (req, res) => {
const cb = typeof fn === 'function' ? fn(req, res) : fn;
const send = fn => (req, res, next) => {
const cb = typeof fn === 'function' ? fn(req, res, next) : fn;

new Promise(resolve => resolve(cb)).then(val => {
if (val) {
Expand Down Expand Up @@ -48,8 +48,15 @@ const createTestServer = opts => createCert(opts && opts.certificate)
})
]);

app.get = (path, fn) => {
get(path, send(fn));
app.get = function () {
// TODO: Use destructuring when targeting Node.js v6
const args = Array.from(arguments);
const path = args[0];
const fns = args.slice(1);

for (const fn of fns) {
get(path, send(fn));
}
};

return app.listen().then(() => app);
Expand Down
12 changes: 12 additions & 0 deletions test/create-test-server.js
Expand Up @@ -121,3 +121,15 @@ test('support returning body directly without wrapping in function', async t =>
t.deepEqual(bodyJson, { foo: 'bar' });
t.deepEqual(bodyAsync, 'bar');
});

test('accepts multiple callbacks in `.get()`', async t => {
const server = await createTestServer();

server.get('/foo', (req, res, next) => {
res.set('foo', 'bar');
next();
}, (req, res) => res.get('foo'));

const { body } = await got(server.url + '/foo');
t.is(body, 'bar');
});

0 comments on commit 23319da

Please sign in to comment.