P.Nuzum
P.Nuzum

Reputation: 129

Express Router "Cannot GET ..." on browser screen

I'm trying to set up a router.get that is in another folder rather than /routes. When I direct browser to this route, I get "Cannot GET /auth_box" on browser screen. Either you can't so this, or I'm doing something dumb.

app.js:

var index = require('./routes/index');
var auth_box = require('./public/js/download_cs');
app.use('/', index);
app.use('/auth_box', auth_box);

download_cd.js:

var express = require('express');
var app = express();
var router = express.Router();
router.get('/auth_box', function(req, res){
  console.log("/auth_box");
});
module.exports = router;

Upvotes: 9

Views: 9457

Answers (2)

Pranesh Ravi
Pranesh Ravi

Reputation: 19133

Your download_cd.js should be like the following. By using, app.use('/auth_box', auth_box); your are specifying /auth_box as the base path for all the routes in auth_box

var express = require('express');
var app = express();
var router = express.Router();
router.get('*', function(req, res){ //this matches /auth_box/*
  console.log("/auth_box");
});
router.get('/sample', function(req, res){ //this matches /auth_box/sample
  console.log("/auth_box/sample");
});
module.exports = router;

Upvotes: 4

adeneo
adeneo

Reputation: 318312

You have the url /auth_box twice.
When you use a route, the first argument is the default path for that route, so right now the correct URL would be /auth_box/auth_box

In your route, just do

router.get('/', function(req, res){
  console.log("/auth_box");
});

As you've already set /auth_box in app.use('/auth_box', auth_box);

Upvotes: 14

Related Questions