' router.get' vs' router.route.get'在express.js

时间:2015-11-09 20:29:41

标签: javascript node.js express

我刚刚开始学习MEAN堆栈并通过我有文件的教程,api.js和auth.js。

在api.js中,我有以下路线结构,

    var express = require('express');
    var router = express.Router();    

    //Used for routes that must be authenticated.
    function isAuthenticated (req, res, next) {
        // if user is authenticated in the session, call the next() to call the next request handler 
        // Passport adds this method to request object. A middleware is allowed to add properties to
        // request and response objects

        //allow all get request methods
        if(req.method === "GET"){
        console.log('in console');
            return next();
        }
        if (req.isAuthenticated()){
            return next();
        }

        // if the user is not authenticated then redirect him to the login page
        return res.redirect('/#login');
    };

    //Register the authentication middleware
    router.use('/posts', isAuthenticated);
    router.route('/posts')

    .get(function(req,res){
    res.send({message:'TODO: return all posts'});
    })

    .post(function(req,res){
    res.send({message:'TODO: create new post'});
    });

    router.route('/posts/:id')

    .get(function(req,res){
    res.send({message:'TODO: return post with ID ' + req.params.id});
    })

    .put(function(req,res){
    res.send({message:'TODO: modify post with ID ' + req.params.id});
    })

    .delete(function(req,res){
    res.send({message:'TODO: delete post with ID ' + req.params.id});
    });
    module.exports = router;

在auth.js中,我有以下路线结构,

var express = require('express');
var router = express.Router();

module.exports = function(passport){

    //sends successful login state back to angular
    router.get('/success', function(req, res){
        res.send({state: 'success', user: req.user ? req.user : null});
    });

    //sends failure login state back to angular
    router.get('/failure', function(req, res){
        res.send({state: 'failure', user: null, message: "Invalid username or password"});
    });

    //log in
    router.post('/login', passport.authenticate('login', {
        successRedirect: '/auth/success',
        failureRedirect: '/auth/failure'
    }));

    //sign up
    router.post('/signup', passport.authenticate('signup', {
        successRedirect: '/auth/success',
        failureRedirect: '/auth/failure'
    }));

    //log out
    router.get('/signout', function(req, res) {
        req.logout();
        res.redirect('/');
    });

        return router;    
}

上面的代码工作正常,但每当我尝试重写api.js的代码,如下面的auth.js结构时,

module.exports = function(){

router.get('/posts',function(req,res)
{
  res.send({message:'TODO: return all posts'});
});

router.post('/posts',function(req,res)
{
  res.send({message:'TODO: add new post'});
});

router.get('/posts/:id',function(req,res)
{
  res.send({message:'TODO: return post with ID ' + req.params.id});
});

router.put('/posts/:id',function(req,res)
{
  res.send({message:'TODO: edit post with ID ' + req.params.id});
});

router.delete('/posts/:id',function(req,res)
{
  res.send({message:'TODO: delete post with ID ' + req.params.id});
});

return router;
}

这不起作用。以下是每当我发布任何帖子或获取请求时节点cmd提示的屏幕截图。我是否以错误的方式重写代码?

enter image description here

1 个答案:

答案 0 :(得分:1)

您现在正在api.js导出功能与路由器实例。如果您没有相应地更改使用api.js的文件,您将安装一个函数而不是路由器。

因此,对于新的api.js,您的父文件需要执行以下操作:

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

而不是像:

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