简体   繁体   English

单个.js文件中的快速路由

[英]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. 我是Node / Express的新手,我认为这是一个简单的问题,但如果我的逻辑/理解是合理的,我就不太了解。

Here's a simplified version of my app structure: 这是我的app结构的简化版本:

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

I've got my routes defined in my app.js file like this. 我已经在我的app.js文件中定义了我的路由。

// 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: 我想将来自/ filterone/ filtertwo的流量指向我的filter.js路由,然后在路由中我想像这样处理它们:

// 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. 您将在filter.js定义的路线添加到应用程序的路线中,并附加'/filterone' Since you define a filter '/filterone' route (I assume this was in filter.js ). 由于您定义了一个过滤器'/filterone'路由(我假设这是在filter.js )。 This means to access the route you would need to query /filterone/filterone . 这意味着访问查询/filterone/filterone所需的路由。

Andreas Rau's answer would work but if you want multiple route files you could just make one small addition. Andreas Rau的答案可行,但如果你想要多个路径文件,你可以做一个小的补充。

//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 . 这样您就可以使用/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 . 通过这种方式,您可以将它们分别显示在各自的文件中,索引显示路径为/show ,过滤器显示路径为/filter/show

Edit* Here is an example for your routes file's based on Andreas Rau's answer and using subroutes: 编辑*以下是基于Andreas Rau的回答和使用子路由的路由文件的示例:

routes/filter.js : 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 : 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: 现在您可以在app.js代码中使用它:

 //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)! 编辑:您可以使用这样的多个路由器,您只需要更改基本路径(app.use的第一个参数)! So basically you could create an index router, a filterone router and so on. 所以基本上你可以创建一个索引路由器,一个filterone路由器等等。

both of your filterone and filtertwo are like subroutes from filter, so in your app you only need to: 你的filterone和filtertwo都像过滤器中的子路由,所以在你的应用中你只需要:

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文件并在那里抨击路线

filter.js example: filter.js示例:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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