Blake
Blake

Reputation: 374

Getting a 404 on a route alias

I'm new to Node and angular and trying to figure out why my app is throwing a 404. Here is the main error that I see in Firebug:

 Cannot GET /api/user

In my server.js I make a call to the routes:

 var express = require('express');
 var app = express();
 require('./routes')(app)

Within the routes folder I have index.js (which looks like it is called with the above require).

 module.exports = function(app) {
    app.use(restUtils.buildUrl('/api/user'), require('./user-api'));
 }

restUtils just returns: '/api/user' and user-api is just another route within that same routes folder. I can place a console.log right within the module.exports so I can see its making it there on server startup. It appears like it is generating the alias to api/user. However, when the app goes to find api/user it returns the 404.

Here is a snippet from user-api.js:

 var router = require('express').Router(),
     config = require('../conf/config'),
     logger = require('../lib/logger'),
     restUtils = require('../lib/rest-utils');
 console.log("Inside User-Api");
 router.get('/', function(req,res) {
    console.log("Inside user-api - router.get");
    if (config.debug && config.secured) logger.info('USER: ', req.user)
    var user = {
        id: req.user ? req.user.id : '',
        displayName: req.user ? req.user.displayname : 'Unknown User',
        roles: req.user ? req.user.appRoles : [],
        groups: req.user ? req.user.appGroups : [],
        profile: req.user ? req.user.profile : {}
    };

    res.format({
       json: function() {
          res.send(user);
       }
    });
 });

 module.exports = router;

I see the first console.log on server startup but it never falls into router.get.

Please let me know if you think I should show more code at different spots - just not sure what is needed to try and figure out this 404! Maybe the user-api route is failing to load when it should be calling the router.get? I think I just don't know enough about routing and modules just yet. Thanks!!

Upvotes: 0

Views: 148

Answers (2)

feiiiiii
feiiiiii

Reputation: 1583

The problem exist at

restUtils.buildUrl('/api/user') 

which is returning a string 'api/user', it needs to be '/api/user'

Upvotes: 1

N1klash
N1klash

Reputation: 1

The error says:

Cannot GET /api/user

So it tries to get a route which doesn't exists. You could add a get route for /api/user, that would fix the error I guess.

app.get("/api/user",function(req,res){})

I hope thats may help you

Upvotes: 0

Related Questions