Sails自定义路由

Me *_*agi 1 sails.js

以下是我的应用程序的自定义路由.有没有更好的方法或组织它们?我不想在添加新模块时创建一组自定义路由.理想情况下,我希望在不定义其中任何一个的情况下采取行动.由于我的网址深度不足,我不会利用默认的Controller/Action /:id路由.

添加大量自定义路线是否会影响应用性能?帮助赞赏.

'get /location/:locationId/equipment/new'         : 'LocationController.equipmentNew',
'get /location/:locationId/equipment/create'      : 'LocationController.equipmentCreate',
'get /location/:locationId/equipment/edit/:id'    : 'LocationController.equipmentEdit',
'get /location/:locationId/equipment/update/:id'  : 'LocationController.equipmentUpdate',
'get /location/:locationId/equipment/list'        : 'LocationController.equipmentList',
'get /location/:locationId/equipment/show/:id'    : 'LocationController.equipmentShow',
'get /location/:locationId/equipment/delete/:id'  : 'LocationController.equipmentDelete',

'get /location/:locationId/employee/new'          : 'LocationController.employeeNew',
'get /location/:locationId/employee/create'       : 'LocationController.employeeCreate',
'get /location/:locationId/employee/edit/:id'     : 'LocationController.employeeEdit',
'get /location/:locationId/employee/update/:id'   : 'LocationController.employeeUpdate',
'get /location/:locationId/employee/list'         : 'LocationController.employeeList',
'get /location/:locationId/employee/show/:id'     : 'LocationController.employeeShow',
'get /location/:locationId/employee/delete/:id'   : 'LocationController.employeeDelete',

'get /location/:locationId/manager/new'           : 'LocationController.managerNew',
'get /location/:locationId/manager/create'        : 'LocationController.managerCreate',
'get /location/:locationId/manager/edit/:id'      : 'LocationController.managerEdit',
'get /location/:locationId/manager/update/:id'    : 'LocationController.managerUpdate',
'get /location/:locationId/manager/list'          : 'LocationController.managerList',
'get /location/:locationId/manager/show/:id'      : 'LocationController.managerShow',
'get /location/:locationId/manager/delete/:id'    : 'LocationController.managerDelete',
'get /location/:locationId/participate'           : 'LocationController.participate',
Run Code Online (Sandbox Code Playgroud)

abe*_*eja 6

是的,这是可能的,但你必须使用快速中间件编写自己的请求路由器.

在SailsJS中,每个策略都是一个中间件.您可以将自定义参数与URL中的控制器和操作名称一起传递,并直接从策略中调用相关的控制器操作.

首先,您需要使用和不使用id参数定义操作的路由

配置/ routes.js

/locations/:locationId/:controller/:action : { policy: 'router' }
/locations/:locationId/:controller/:action/:id : { policy: 'router' }
Run Code Online (Sandbox Code Playgroud)

接下来,您需要编写代码,以根据URL参数将请求重定向到控制器和操作.

API /政策/ router.js

module.exports = function (req, res, next) {
    var controller = req.param('controller');
    var action = req.param('action');
    sails.controllers[controller][action](req, res);
};
Run Code Online (Sandbox Code Playgroud)

然后,您可以在单独的文件中定义控制器:

api/controllers/EmployeeController.js&api/controllers/EquipmentController.js

module.exports = {
    new: function(req, res) {
        var params = req.params.all();
        // ...
    },
    // ... other actions
};
Run Code Online (Sandbox Code Playgroud)

重要提示:请记住保护您不想暴露的行为!

希望有所帮助.