2016-02-27 40 views
0

在Node JS中做了一個登錄類型的事情,並試圖使用JSON令牌。這裏是server.js:節點JS Json Web令牌Router.use()需要中間件功能,但有一個未定義的

// dependancies 
var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 
var morgan = require('morgan'); 
var mongoose = require('mongoose'); 
var passwordHash = require('password-hash'); 

var jwt = require('jsonwebtoken'); 
var config = require('./config'); 
var User = require('./app/models/user'); 

// config 

var port = process.env.PORT || 8080; 
mongoose.connect(config.database); 
app.set('superSecret', config.secret); 

// body parser 
app.use(bodyParser.urlencoded({ 
    extended: false 
})); 
app.use(bodyParser.json()); 

// morgan 
app.use(morgan('dev')); 

//routes 
app.get('/', function(req, res) { 
    res.send('Hello! The API is at http://localhost:' + port + '/api'); 
}); 

app.get('/setup', function(req, res) { 
    var hashedPassword = passwordHash.generate('catsanddogs'); 
    var tommy = new User({ 
    user: 'Tommy Birt', 
    password: hashedPassword, 
    admin: false, 
    MP: true, 
    Position: true 
    }); 
    // save 
    tommy.save(function(err) { 
    if (err) throw err; 

    console.log('Saved'); 
    res.json({ 
     success: true 
    }); 
    }); 
}); 

// API routes 
var apiRoutes = express.Router(); 

apiRoutes.get('/', function(req, res) { 
    res.json({ 
    message: 'Welcome to this api.' 
    }); 
}); 


apiRoutes.post('/authenticate', function(req, res) { 
    // Fine user 
    User.findOne({ 
    user: req.body.user 
    }, function(err, user) { 
    // general error... 
    if (err) throw err; 

    // incorrect 
    if (!user) { 
     res.json({ 
     success: false, 
     message: 'Authentication failed: No user' 
     }); 
    } else if (user) { 
     if (!passwordHash.verify(req.body.password, user.password)) { 
     res.json({ 
      success: false, 
      message: 'Authentication failed: Incorrect Password' 
     }); 
     } else { 
     // token! 
     var token = jwt.sign(user, app.get('superSecret'), { 
      expiresIn: 64000 
     }); 

     res.json({ 
      success: true, 
      message: 'Enjoy your token!', 
      token: token 
     }); 
     } 
    } 
    }) 
}); 
app.use('/api', apiRoutes); 

apiRoutes.use(function(req, res, next) { 
    // Lets find our token! 
    var token = req.body.token || req.query.token || req.headers['x-access-token']; 

    // Have we found it? 
    if (token) { 
    // Verify! No crackers please 
    jwt.verify(token, app.get('superSecret'), function(err, decoded) { 
     if (err) { 
     // Somethings gone wrong... 
     return res.json({ 
      sucess: false, 
      message: 'Failed to authenticate token' 
     }); 
     } else { 
     // No errors! Yay :) 
     req.decoded = decoded 
     next(); 
     } 
    }); 
    } else { 
    // No token Sir! Get off this train! 
    return res.satus(403).send({ 
     success: false, 
     message: 'Stop cheating' 
    }); 
    } 
}); 

apiRoutes.use('/bank', bankRoutes); 

var bankRoutes = express.Router(); 
bankRoutes.get('/balance', function(req, res) { 
    res.send("Welcome to your balance!"); 
}); 

// lets roll 
app.listen(port); 
console.log('We have taken off at http://localhost' + port); 

基本上,只要我跑,沒有導航到任何東西,它拋出了這個錯誤:類型錯誤:Router.use()需要中間件的功能,但有一個未定義的。

發生了什麼事?爲什麼這個錯誤被拋出?

+0

您可以發佈您的模型user.js的文件以及.. –

回答

2

的問題可能是你bankRoutes -

apiRoutes.use('/bank', bankRoutes); 

var bankRoutes = express.Router(); 
bankRoutes.get('/balance', function(req, res) { 
    res.send("Welcome to your balance!"); 
}); 

你定義它之前調用bankRoutes

所以更改它像這樣(先定義它不是用它) -

var bankRoutes = express.Router(); 
bankRoutes.get('/balance', function(req, res) { 
    res.send("Welcome to your balance!"); 
}); 

apiRoutes.use('/bank', bankRoutes); 
相關問題