Skip to content

Commit f972089

Browse files
committed
Auto configure routes using a file name pattern
1 parent 2c6d727 commit f972089

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

package-lock.json

Lines changed: 23 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@types/convict": "^4.2.1",
2323
"@types/errorhandler": "0.0.32",
2424
"@types/express": "^4.17.1",
25+
"@types/glob": "^7.1.1",
2526
"@types/helmet": "0.0.44",
2627
"@types/node": "~11.13.0",
2728
"@types/uuid": "^3.4.5",
@@ -32,6 +33,7 @@
3233
"copy": "^0.3.2",
3334
"errorhandler": "^1.5.1",
3435
"express": "^4.17.1",
36+
"glob": "^7.1.6",
3537
"helmet": "^3.21.1",
3638
"http-status": "^1.3.2",
3739
"mandrill-api": "^1.0.45",

src/apps/mooc_backend/routes/create-course.route.ts renamed to src/apps/mooc_backend/routes/courses.route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Express } from 'express';
22
import container from '../config/dependency-injection';
33
import CreateCourseController from '../controllers/CreateCourseController';
44

5-
export const createUserRoute = (app: Express) => {
5+
export const register = (app: Express) => {
66
const controller: CreateCourseController = container.get('Apps.mooc.controllers.CreateCourseController');
7-
app.put('/courses/:id', (req, res) => controller.create(req, res));
7+
app.put('/courses/:id', controller.create.bind(controller));
88
};
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { Express } from 'express';
2-
import { statusRoute } from './status.route';
3-
import { createUserRoute } from './create-course.route';
2+
import glob from 'glob';
43

54
export function registerRoutes(app: Express) {
6-
statusRoute(app);
7-
createUserRoute(app);
5+
const routes = glob.sync(__dirname + '/**/*.route.*');
6+
routes.map(route => register(route, app));
7+
}
8+
9+
function register(routePath: string, app: Express) {
10+
const route = require(routePath);
11+
route.register(app);
812
}

src/apps/mooc_backend/routes/status.route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Express } from 'express';
22
import container from '../config/dependency-injection';
33
import StatusController from '../controllers/StatusController';
44

5-
export const statusRoute = (app: Express) => {
5+
export const register = (app: Express) => {
66
const controller: StatusController = container.get('Apps.mooc.controllers.StatusController');
7-
app.get('/status', (req, res) => controller.create(req, res));
7+
app.get('/status', controller.create.bind(controller));
88
};

0 commit comments

Comments
 (0)