Pritam
Pritam

Reputation: 1400

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

I have just started learning MEAN stack and going through tutorial in which I have files, api.js and auth.js.

In api.js I have below route structure,

    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;

In auth.js I have below route structure,

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;    
}

Above code works fine but whenever I try to rewrite code of api.js like auth.js structure below,

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;
}

This doesn't work. Below is the screen shot of the node cmd prompt whenever I make any post or get request. Am I rewriting code in wrong manner ?

enter image description here

Upvotes: 1

Views: 3394

Answers (1)

mscdex
mscdex

Reputation: 106736

You're exporting a function vs. a Router instance in api.js now. If you didn't change the file that uses api.js accordingly, you will be mounting a function instead of a Router.

So for the new api.js, your parent file will need to be doing something like:

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

instead of something like:

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

Upvotes: 1

Related Questions