Browse Source

Accept multiple middlewares in `app.get` (#16)

greenkeeper/xo-0.20.3
Kevin Mårtensson 7 years ago
committed by Luke Childs
parent
commit
23319da689
  1. 13
      src/index.js
  2. 12
      test/create-test-server.js

13
src/index.js

@ -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) {
@ -48,8 +48,15 @@ const createTestServer = opts => createCert(opts && opts.certificate)
})
]);
app.get = (path, 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);

12
test/create-test-server.js

@ -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');
});

Loading…
Cancel
Save