app.use不会被解雇

时间:2017-05-16 14:57:39

标签: javascript node.js express

我正在尝试使用我的应用程序中的中间件检查所有路由,如果令牌被传递,此刻我想用/ users路由测试它,所以我在我的应用程序文件上做了这个:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var debug = require('debug')('express-sequelize');
var http = require('http');
var models = require('./models');
var jwt = require('./routes/jwt');
var app = express();


// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/',require('./routes/authentication'));

app.use('/users',jwt); // executa a verificacao do token em todas as rotas excepto o registo e login


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

然后在我的jwt文件中我做了这个:

var express = require('express');
var User = require('../models').User;
var router = express.Router();
var jwt = require('jsonwebtoken');

// route middleware to verify a token
module.exports = function () {
    return function (req, res, next) {
        console.log("entered");
        // check header or url parameters or post parameters for token
        var token = req.body.token || req.query.token || req.headers['x-access-token'];
        // decode token
        if (token) {
            // verifies secret and checks exp
            jwt.verify(token, app.get('superSecret'), function (err, decoded) {
                if (err) {
                    return res.json({ success: false, message: 'Failed to authenticate token.' });
                } else {
                    // if everything is good, save to request for use in other routes
                    req.decoded = decoded;
                    next();
                }
            });
        } else {
            // if there is no token
            // return an error
            return res.status(403).send({
                success: false,
                message: 'No token provided.'
            });

        }
    }
};

我的jwt文件位于与app文件处于同一级别的routes文件夹中, 任何消化?它没有击中路线

1 个答案:

答案 0 :(得分:0)

您的jwt文件未正确设置,请将其更改为

var express = require('express');
var User = require('../models').User;
var router = express.Router();
var jwt = require('jsonwebtoken');

router.get('/', function(req, res, next) {
    console.log("entered");

    // check header or url parameters or post parameters for token
    var token = req.body.token || req.query.token || req.headers['x-access-token'];
    // decode token
    if (token) {
        // verifies secret and checks exp
        jwt.verify(token, app.get('superSecret'), function (err, decoded) {
            if (err) {
                return res.json({ success: false, message: 'Failed to authenticate token.' });
            } else {
                // if everything is good, save to request for use in other routes
                req.decoded = decoded;
                next();
            }
        });
    } else {
        // if there is no token
        // return an error
        return res.status(403).send({
            success: false,
            message: 'No token provided.'
        });

    }
})

module.exports = router;