forked from Hackbyrd/express-master-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
41 lines (31 loc) · 1.17 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
35
36
37
38
39
40
41
/**
* All routes are placed in this file
* All urls are going to be lowercase
* All urls will not have underscore or dashes or spaces
*/
'use strict';
// require built-in node modules
const fs = require('fs');
const path = require('path');
// require third-party modules
const express = require('express');
let router = express.Router();
// variables
const APP_DIR = './app'; // app directory
const ROUTE_FILE = 'routes.js'; // the route file name
// Store all app routes here
const appRoutes = [];
// check if is directory and get directories
const isDirectory = source => fs.lstatSync(source).isDirectory();
const getDirectories = source => fs.readdirSync(source).map(name => path.join(source, name)).filter(isDirectory);
const directories = getDirectories(path.join(__dirname, APP_DIR));
// require app routes
directories.forEach(dir => appRoutes.push(require(`${dir}/${ROUTE_FILE}`)));
module.exports = passport => {
// Welcome
router.all('/', (req, res) => res.status(200).send(`API v1.0.0 - ${req.__('GLOBAL[Language]')}`));
// All app routes are automatically inserted here
appRoutes.forEach(r => (router = r(passport, router)));
// return router
return router;
};