快速4路线设置

时间:2014-12-12 20:18:50

标签: node.js express

我试图正确而干净地设置我的Express路线,这是我的routes.js文件:

var tokenCtrl = require('./routes/token');
var expressJwt = require('express-jwt');
var config = require('./config');

module.exports = function (app) {  
    app.route('/api/token')
        .all(expressJwt({
            secret: config.jwtPrivateKey,
            userProperty: 'auth' //JWT payload will be bound to this value (req.{value})
        }))
        .post('validate', tokenCtrl.validate)
        .get('generate', tokenCtrl.generate);
}

不幸的是,我无法在.post或.get中指定路由后缀(如上所述)。所以我坚持做这样的事情:

var tokenCtrl = require('./routes/token');
var expressJwt = require('express-jwt');
var config = require('./config');

module.exports = function (app) {    
    app.route('/api/token/validate')
        .all(expressJwt({
            secret: config.jwtPrivateKey,
            userProperty: 'auth'
        }))
        .post(tokenCtrl.validate);

    app.route('/api/token/generate')
        .all(expressJwt({
            secret: config.jwtPrivateKey,
            userProperty: 'auth'
        }))
        .get(tokenCtrl.generate);
}

现在这非常麻烦。有没有办法说我的所有路由都有/api前缀,然后为每个控制器添加另一个前缀?还有更好的方法吗?

2 个答案:

答案 0 :(得分:2)

您可以使用单独的路由器为您的api express.Router();

var express = require('express');
var tokenCtrl = require('./routes/token');
var expressJwt = require('express-jwt');
var config = require('./config');

var router = express.Router();

router.route('/token/validate')
  .all(expressJwt({
    secret: config.jwtPrivateKey,
    userProperty: 'auth'
  }))
  .post(tokenCtrl.validate);

router.route('/token/generate')
  .all(expressJwt({
    secret: config.jwtPrivateKey,
    userProperty: 'auth'
  }))
  .get(tokenCtrl.generate);

module.exports = router;

然后在你的app.js中你可以做到。

var express = require('express');
var apiRouter = require('./api');

app.use('/api', apiRouter);

然后您可以执行相同的/token/validate/generate

答案 1 :(得分:2)

您可以做的是,为ExpressJwt创建一个通用的中间件功能,它将自动传递到所有路由。并且在您的路线文件中仅为' / token / validate'制作终点。和' / token / generate'使用Express Router的路由

var express = require('express');
var tokenCtrl = require('./routes/token');
var expressJwt = require('express-jwt');
var config = require('./config');
var router = express.Router();

// make middleware for expressJwt
router.use(expressJwt({
    secret: config.jwtPrivateKey,
    userProperty: 'auth'
  }));
// define the token validate route 
router.post('/token/validate',tokenCtrl.validate );

// define the token generate route
router.get('/token/generate', tokenCtrl.generate)

module.exports = router;

然后,在应用程序中加载此路由器模块,现在前缀' / api'将自动包含在进一步的“令牌/验证”中。并且'令牌/生成'端点

var tokenroutes= require('./routesjs');
app.use('/api', tokenroutes);

或者,如果您不想在某些路线中包含expressJwt,则可以执行此操作

像expressJwt中间件只能传递到vaildate路径

var express = require('express');
var tokenCtrl = require('./routes/token');
var expressJwt = require('express-jwt');
var config = require('./config');
var router = express.Router();

// make middleware for expressJwt
ej = expressJwt({
    secret: config.jwtPrivateKey,
    userProperty: 'auth'
  });
// define the token validate route ,
router.post('/token/validate',ej,tokenCtrl.validate );

// define the token generate route
router.get('/token/generate', tokenCtrl.generate)

module.exports = router;