3

I'm new to Node/Express, and I think this is a simple ask, but I'm not quite if my logic/understanding is sound.

Here's a simplified version of my app structure:

/app
  /routes
    filter.js
    index.js
  app.js

I've got my routes defined in my app.js file like this.

// app.js

app.use('/', require('./routes/index'));
app.use('/filterone', require('./routes/filter'));
app.use('/filtertwo', require('./routes/filter'));

I'd like to point traffic from both /filterone and /filtertwo to my filter.js route, and then in the route I'd like to handle them like this:

// filter.js

router.get('/filterone', function(req, res, next) {
  // do something
}

router.get('/filtertwo', function(req, res, next) {
  // do something
}

Is that the correct way to go about doing this? Or should I be handling my routes differently?

3 Answers 3

2

I don't believe the code you have will accomplish what you are trying to accomplish. When you do:

app.use('/filterone', require('./routes/filter'));

You are adding the routes you define in filter.js to your app's routes with '/filterone' pre-appended. Since you define a filter '/filterone' route (I assume this was in filter.js). This means to access the route you would need to query /filterone/filterone.

Andreas Rau's answer would work but if you want multiple route files you could just make one small addition.

//app.js

const express = require('express'),
      index = require('./routes/index')
      filters = require('./routes/filter'),
      app = express();

app.use('/', index);
app.use('/filter', filters);

This way you can access your filter routes with /filter/route_name. This will free you up to call different routes the same thing if they are in different route files. For example, you might want to show a filter and show something in your index. This way you can call them both show in their respective files and the index show route is /show and the filter show route is /filter/show.

Edit* Here is an example for your routes file's based on Andreas Rau's answer and using subroutes:

routes/filter.js:

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

router.get('/show',(req,res) => {
 //handle /filter/show
} );


router.get('/filterone',(req,res) => {
 //handle /filter/filterone
} );

router.get('/filtertwo',(req,res) => {
 //handle /filter/filtertwo
} );

module.exports = router;

routes/index.js:

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

router.get('/show',(req,res) => {
 //handle /show
} );

module.exports = router;
3
  • How would I account for multiple routes in my filter.js file then?
    – knoll
    Dec 6, 2016 at 20:30
  • I added an example to my question, let me know if it works for you. Dec 6, 2016 at 22:03
  • Ah, now it makes sense. Thanks morsecodist!
    – knoll
    Dec 6, 2016 at 22:08
0

//filter.js

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

router.get('/',(req,res) => {
 //code...
} );


router.get('/filterone',(req,res) => {
 //code...
} );

router.get('/filtertwo',(req,res) => {
 //code...
} );

module.exports = router;

Now you can use it in your app.js code:

//filter.js

const express = require('express'),
      router = require('./route/filter'),
      app = express();

app.use('/',router);

EDIT: You can use multiple routers like this you just have to change the base path (first parameter of app.use)! So basically you could create an index router, a filterone router and so on.

0

both of your filterone and filtertwo are like subroutes from filter, so in your app you only need to:

app.use('/filter', require('./routes/filter'));

then inside your routes folder, you will create the filter.js file and slam the routes there

filter.js example:

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

//optional if you want to use controllers or not
var controller = require('path to controller') 

//in case of controllers
router.get('/filterone', controller.funcToTakeCareOfFilterone);
router.get('/filtertwo', controller.funcToTakeCareOfFiltertwo);

//in case of no controllers
router.get('/filterone', function(req, res , next) {
  //your code
});
router.get('/filtertwo', function(req, res , next) {
 //your code
});

module.exports = router;
1
  • I get a 404 for both routes when I try this method.
    – knoll
    Dec 6, 2016 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.