-
Notifications
You must be signed in to change notification settings - Fork 8
/
routes.js
34 lines (33 loc) · 1.32 KB
/
routes.js
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
const fs = require('fs');
const path = require('path');
const klaw = require('klaw');
const through2 = require('through2');
module.exports = function (app,config,db,proxy,c9,cryptr) {
return function loadRoutes(dir) {
return new Promise((resolve,reject) => {
klaw(dir,{depthLimit: -1})
.pipe(through2.obj(function (item, enc, next) {
if (!item.stats.isDirectory()) this.push(item);
next();
}))
.on('data',function (file) {
var route = file.path.replace(path.resolve('routes'),'').replace('.js','').replace('index','').replace('+','/');
var run = require(file.path)(app, config, db, proxy, c9, cryptr);
app.all(route,(req,res) => {
if ('all' in run) {
run['all'](req,res);
} else {
if (req.method.toLowerCase() in run) {
run[req.method.toLowerCase()](req,res);
} else if ('else' in run) {
run['else'](req,res);
} else {
res.send('Invalid Method.');
}
}
});
})
.on('end',resolve);
});
}
}