Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 222
P.Nuzum Avatar asked Oct 28 '16 14:10

P.Nuzum


2 Answers

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

like image 111
adeneo Avatar answered Nov 11 '22 17:11

adeneo


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;
like image 4
Pranesh Ravi Avatar answered Nov 11 '22 17:11

Pranesh Ravi