简体   繁体   中英

Express Routes in a Single .js File

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?

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;

 //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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM