Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use glob to match *.route files #453

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions index.route.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
const express = require('express');
const userRoutes = require('./server/user/user.route');
const authRoutes = require('./server/auth/auth.route');
const glob = require('glob');
const path = require('path');

const router = express.Router(); // eslint-disable-line new-cap

// TODO: use glob to match *.route files

/** GET /health-check - Check service health */
router.get('/health-check', (req, res) =>
res.send('OK')
);

// mount user routes at /users
router.use('/users', userRoutes);
// Mount routes defined in *.route.js files
mountRoutes();

/**
* Mounts all routes defined in *.route.js files in server/
* @example
* auth.route.js routes will be mounted to /auth
* @example
* user.test.js routes will be mounted to /user
*/
function mountRoutes() {
// Route definitions
const files = glob.sync('server/**/*.route.js');

// Mount routes for each file
files.forEach((routeFilename) => {
const routes = require(`./${routeFilename}`); // eslint-disable-line global-require

// Create the url using the first part of the filename
// e.g. auth.route.js will generate /auth
const routeName = path.basename(routeFilename, '.route.js');
const url = `/${routeName}`;

// mount auth routes at /auth
router.use('/auth', authRoutes);
// Mount the routes
console.info(`${path.basename(routeFilename)} -> ${url}`); // eslint-disable-line no-console
router.use(url, routes);
});
}

module.exports = router;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"express-jwt": "5.3.1",
"express-validation": "1.0.2",
"express-winston": "2.5.0",
"glob": "false7.1.2",
"helmet": "3.12.0",
"http-status": "1.0.1",
"joi": "10.6.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const User = require('./user.model');
const User = require('./users.model');

/**
* Load user and append to req.
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions server/user/user.route.js → server/users/users.route.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const express = require('express');
const validate = require('express-validation');
const paramValidation = require('../../config/param-validation');
const userCtrl = require('./user.controller');
const usersCtrl = require('./users.controller');

const router = express.Router(); // eslint-disable-line new-cap

router.route('/')
/** GET /api/users - Get list of users */
.get(userCtrl.list)
.get(usersCtrl.list)

/** POST /api/users - Create new user */
.post(validate(paramValidation.createUser), userCtrl.create);
.post(validate(paramValidation.createUser), usersCtrl.create);

router.route('/:userId')
/** GET /api/users/:userId - Get user */
.get(userCtrl.get)
.get(usersCtrl.get)

/** PUT /api/users/:userId - Update user */
.put(validate(paramValidation.updateUser), userCtrl.update)
.put(validate(paramValidation.updateUser), usersCtrl.update)

/** DELETE /api/users/:userId - Delete user */
.delete(userCtrl.remove);
.delete(usersCtrl.remove);

/** Load user when API with userId route parameter is hit */
router.param('userId', userCtrl.load);
router.param('userId', usersCtrl.load);

module.exports = router;
File renamed without changes.