0

I've recently moved all my route code into separate files, but now my route resolution is spotty. Here's a simple example -

My app.js

const express = require('express');
const app = express();

var dataLoader = require('./routes/dataLoader');
app.all( '/api/load', dataLoader);

My dataLoader.js

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

router.get('/api/load', (req, res) => {
    res.send('Hello Loader...');
});

router.get('/api/load/bob', (req, res) => {
   res.send('Hello Loader - Bob...');
});

router.get('/api/load/jim', (req, res) => {
   res.send('Hello Loader - Jim...');
});

module.exports = router;

/api/load works fine, while /api/load/jim and /api/load/bob both result in:

Cannot GET /api/load/jim (or Cannot GET /api/load/bob, respectively) 

I user app.all() instead of app.use() because I was having an issue resolving the main path "/api/load", the use of all seemed to fix that, but now I am not sure.

"engines": { "node": "^8.9.1" }, "dependencies": { "bluebird": "^3.5.1", "body-parser": "^1.15.1", "express": "^4.13.4", "mongoose": "4.9.8" } any suggestions?

j-p
  • 3,698
  • 9
  • 50
  • 93
  • 1
    You just defined a route for `/api/load/api/load`. I doubt that's what you want. Also, you probably want to use `app.use(path, router)`, not `app.all(path, router)`. – jfriend00 Dec 17 '17 at 21:23
  • How about just `router.get('/bob')`? I get the feeling it should be relative to what you supply in app.all – Matt Fletcher Dec 17 '17 at 21:23
  • And yes, if in doubt just use app.use - I've never felt the need to use app.all personally – Matt Fletcher Dec 17 '17 at 21:24
  • 1
    i understand all you suggest, and I concur, it's how i expect it to behave, I'l try again, and get back to ya'll in 3 min – j-p Dec 17 '17 at 21:25
  • All right - that seems to be working now - using app.use() and /jim. I don't know why it didn't work earlier when I was tryng it like that...maybe something else flaky in my code. Thanks. – j-p Dec 17 '17 at 21:33

1 Answers1

1

When you do this:

app.all( '/api/load', dataLoader);

And, then in the dataLoader router, you define routes like this:

router.get('/api/load', (req, res) => {
    res.send('Hello Loader...');
});

What you're actually doing is defining a route for api/load/api/load which is likely not what you want. The paths are cumulative.

The app.use() statement should have whatever common prefix applies to your entire router and then the paths on the router itself should be relative to that. In addition, you should use using app.use(), not app.all() for a router.

So, in your case, change the dataLoader router to this:

// whole router is configured at /api/loader
const router = require('express').Router();

router.get('/', (req, res) => {
    res.send('Hello Loader...');
});

router.get('/bob', (req, res) => {
   res.send('Hello Loader - Bob...');
});

router.get('/jim', (req, res) => {
   res.send('Hello Loader - Jim...');
});

module.exports = router;
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks, I actually tried, when i first moved my code off to route files, to do this - but it erred, or couldn't find the route "cant fond GET .." - but I guess I had an error elsewhere, that I subsequently fixed, and didn't go back to try again. – j-p Dec 18 '17 at 02:26