All files / server/src router.js

74.07% Statements 20/27
66.67% Branches 8/12
75% Functions 6/8
82.61% Lines 19/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 465x 5x   5x                   5x 13x 13x 13x     13x     13x       273x 3x 3x 3x 1x           13x     5x 21x 10x 10x      
let express = require('express');
let loadware = require('loadware');
 
let join = (...middles) => {
  let router = express.Router();
  loadware(middles).forEach(middle => {
    router.use(middle);
  });
  return router;
}
 
 
// TODO: allow concatenation of some as `status()`
const createRouter = (method, path, ...args) => {
  Iif (method === 'del') method = 'delete';
  let router = express.Router();
  router[method](path, ...args);
 
  // This is only experimental right now
  Eif (process.env.EXPERIMENTAL === '1') {
    // Allow to call the response straight in the router:
    //   get('/').send('Hello 世界')
    ['append', 'attachment', 'cookie', 'clearCookie', 'download', 'end', 'file',
      'format', 'json', 'jsonp', 'links', 'location', 'redirect', 'render',
      'send', 'sendFile', 'sendStatus', 'status', 'status', 'type', 'vary'
    ].forEach(type => {
      router[type] = router[type] || ((...extra) => createRouter(method, path, ...args, (req, res, next) => {
        Iif (type === 'file') type = 'sendFile';
        res[type](...extra);
        if (!res.headersSent) {
          next();
        }
      }));
    });
  }
 
  return router;
}
 
module.exports = new Proxy({}, {
  get: (orig, key) => (...args) => {
    Iif (key === 'join') return join(...args);
    return createRouter(key, ...args);
  }
});