I am currently using router.param() to load a resource instance when a specific param is detected:
table.route.js
import express from 'express';
import validate from 'express-validation';
import paramValidation from '../../../config/param-validation';
import tableCtrl from '../controllers/table.controller';
const router = express.Router();
router.route('/:tableId')
/** GET /api/tables/:tableId - Get table */
.get(tableCtrl.get)
/** PUT /api/tables/:tableId - Update table */
.put(validate(paramValidation.updateTable), tableCtrl.update)
/** DELETE /api/tables/:tableId - Delete table */
.delete(tableCtrl.remove);
/** Load table when API with tableId route parameter is hit */
router.param('tableId', tableCtrl.load);
export default router;
table.controller.js
import Table from '../../models/table.model';
function load(req, res, next, id) {
Table.get(id)
.then((table) => {
req.table = table; // eslint-disable-line no-param-reassign
return next();
})
.catch(e => next(e));
}
function get(req, res) {
return res.json(req.table);
}
export default { load, get, ... };
This work well... however, I need to add another route to get only the meta object from the Table model
table.model.js
const TableSchema = new mongoose.Schema({
meta: {...},
columns: [{ ....}]
});
So I can add the following route with 'meta' as a path parameter
router.route('/:tableId/meta')
/** GET /api/tables/:tableId - Get table */
.get(tableCtrl.get)
As 'meta' is a path parameter after the :tableId param, how can I avoid executing the tableCtrl.load function, and rather executing another one like tableCtrl.loadMeta
router.param('tableId', tableCtrl.load); router.param('tableId/meta', tableCtrl.loadMeta); <= this is wrong of course
thanks for your feedback
via erwin
No comments:
Post a Comment