调用所有路由功能,直到调用实际路由并调用不存在的路由

时间:2017-08-14 05:29:14

标签: node.js express routing

在我的快递申请中,我已经定义了一系列路线如下:

import express from 'express';
import passport from 'passport';
import authStrategies from './auth.strategies';
import authController from './auth.controller';
import authService from './auth.service';

let router = express.Router();


router.post('/', authController.create);
router.get('/login', authController.redirectToLogin);
router.post('/login', authController.loginLocal);
router.get('/', authService.verifyOrdinaryUser, authService.verifyAdmin, authController.index);
router.delete('/:id', authService.verifyOrdinaryUser, authService.verifyAdmin, authController.destroy);
router.get('/me', authService.verifyOrdinaryUser, authController.me);
router.put('/:id/password', authService.verifyOrdinaryUser, authController.changePassword);
router.get('/:id', authService.verifyOrdinaryUser, authController.show);
router.get('/google',
    passport.authenticate('google', {
        scope: ['profile']
    }));
router.get('/google/callback',
    passport.authenticate('google'), authController.googleCallback);
module.exports = router;

我将此路由器添加到server.js中的应用程序:

'use strict'
// import the required modules
import express from 'express';
import passport from 'passport';
import config from './config/config';
import logger from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import path from 'path';
import auth from './auth/index'

// connect to db
mongoose.connect(config.db.url, {
    useMongoClient : true
});

// initialize the required variables
let db = mongoose.connection,
    app = express();

// log db connection status
db.on('error', console.error.bind(console,'connection error.'));
db.once('open',() => {
    console.log('Successfully connected to server');
});

//set application variables
app.set("port",config.server.port);

// add middleswares
passport.serializeUser(function(user, done) {
    console.log('serialize ',user);
  done(null, user._id);
});

passport.deserializeUser(function(id, done) {
    console.log('deserialize ',id);
  done(null, id);
});
app.use(passport.initialize());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname,'client')));

//add routes
app.use('/auth', auth.router);
app.use('/', (req, res, next) => {
    res.status(200).json({message : "success"});
})

module.exports = app;

现在,当我向/ auth / google发送GET请求时,它会转到路径中的所有路由,这是我的摩根日志:

GET / 200 6.457 ms - 21
GET /auth/google/callback?code=4/rWjEEB8ZqatELaIYVJTsdg4uBruX3tihoqfh33t54sw - - ms - -
TokenError
    at Strategy.OAuth2Strategy.parseErrorResponse (/Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:329:12)
    at Strategy.OAuth2Strategy._createOAuthError (/Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:376:16)
    at /Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:166:45
    at /Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:191:18
    at passBackControl (/Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
GET /auth/ 403 7.344 ms - 2105
Error: No token provided
    at exports.verifyOrdinaryUser (/Users/dhanushu/Work/Projects/journal/dist/server/auth/auth.service.js:37:19)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at /Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:317:13)
    at /Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:275:10)
    at SendStream.error (/Users/dhanushu/Work/Projects/journal/node_modules/serve-static/index.js:121:7)
GET /auth/google/callback?code=4/rWjEEB8ZqatELaIYVJTsdg4uBruX3tihoqfh33t54sw 500 355.270 ms - 1230
TokenError
    at Strategy.OAuth2Strategy.parseErrorResponse (/Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:329:12)
    at Strategy.OAuth2Strategy._createOAuthError (/Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:376:16)
    at /Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:166:45
    at /Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:191:18
    at passBackControl (/Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
GET /auth/google 403 1.138 ms - 2101
Error: No token provided
    at exports.verifyOrdinaryUser (/Users/dhanushu/Work/Projects/journal/dist/server/auth/auth.service.js:37:19)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at /Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:281:22
    at param (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:354:14)
    at param (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:410:3)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:317:13)
    at /Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:335:12)
GET /auth/google/callback?code=4/rWjEEB8ZqatELaIYVJTsdg4uBruX3tihoqfh33t54sw 500 340.907 ms - 1258
TokenError: Code was already redeemed.
    at Strategy.OAuth2Strategy.parseErrorResponse (/Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:329:12)
    at Strategy.OAuth2Strategy._createOAuthError (/Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:376:16)
    at /Users/dhanushu/Work/Projects/journal/node_modules/passport-oauth2/lib/strategy.js:166:45
    at /Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:191:18
    at passBackControl (/Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/Users/dhanushu/Work/Projects/journal/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
GET /auth/google 403 0.517 ms - 2101
Error: No token provided
    at exports.verifyOrdinaryUser (/Users/dhanushu/Work/Projects/journal/dist/server/auth/auth.service.js:37:19)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at /Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:281:22
    at param (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:354:14)
    at param (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:410:3)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:317:13)
    at /Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/index.js:335:12)

但对于所有其他路线,它运作正常。

POST /auth/ 200 159.107 ms - 1292
GET /auth/me 200 10.090 ms - 129
GET /auth/5991385d081fb00b1b106422 200 4.367 ms - 52
POST /auth/5991385d081fb00b1b106422/password 200 2.339 ms - 21

如果我将/ google路由作为第一个放在路由器文件中,那么它正常工作。我坚持要解决这个问题。

同样,我们可以看到没有名为/ auth / logme的路由,但是当我尝试访问该路由而不是返回404时,它会抛出以下错误。我在这几个小时,但我无法弄清楚原因。

GET /auth/logme 500 20.329 ms - 1930
CastError: Cast to ObjectId failed for value "logme" at path "_id" for model "User"
    at MongooseError.CastError (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/error/cast.js:26:11)
    at ObjectId.cast (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/schema/objectid.js:147:13)
    at ObjectId.castForQuery (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/schema/objectid.js:187:15)
    at cast (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/cast.js:229:32)
    at Query.cast (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/query.js:2742:12)
    at Query.findOne (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/query.js:1354:10)
    at Function.findOne (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/model.js:1343:13)
    at Function.findById (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/model.js:1271:15)
    at exports.show (/Users/dhanushu/Work/Projects/journal/dist/server/auth/auth.controller.js:44:23)
    at Layer.handle [as handle_request] (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dhanushu/Work/Projects/journal/node_modules/express/lib/router/route.js:137:13)
    at /Users/dhanushu/Work/Projects/journal/dist/server/auth/auth.service.js:33:17
    at /Users/dhanushu/Work/Projects/journal/node_modules/jsonwebtoken/verify.js:27:18
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

    at Function.findOne (/Users/dhanushu/Work/Projects/journal/node_modules/mongoose/lib/model.js:1343:13)

1 个答案:

答案 0 :(得分:0)

让我们逐一解决问题。 Express路由器是一个正则表达式,并按路由器顺序匹配url。所以路由器的顺序非常重要。

locations.append(Location(title: "A Location", latitude: 0.0, longitude: 0.0, phone: <#Double#>)) 匹配SO/auth/logme也匹配router.get('/:id')

您应该首先更改路由器的顺序,如下所示。

/auth/google

这样router.get('/:id')现在匹配router.get('/google/callback', passport.authenticate('google'), authController.googleCallback); router.get('/me', authService.verifyOrdinaryUser, authController.me); router.get('/google', passport.authenticate('google', { scope: ['profile'] })); router.get('/login', authController.redirectToLogin); router.get('/:id', authService.verifyOrdinaryUser, authController.show); router.get('/', authService.verifyOrdinaryUser, authService.verifyAdmin, authController.index); router.post('/login', authController.loginLocal); router.post('/', authController.create); router.delete('/:id', authService.verifyOrdinaryUser, authService.verifyAdmin, authController.destroy); router.put('/:id/password', authService.verifyOrdinaryUser, authController.changePassword); 相关问题:Order of router precedence in express.js

如果你想要404 / / auth / logme

/auth/google