简体   繁体   English

Express.js动态路由问题

[英]Expressjs dynamic routing issues

I'm using Express 4.0 and I'm trying to create a routing that show all users from all countries in Homepage. 我使用的是Express 4.0,并且试图创建一个路由,以在首页中显示来自所有国家/地区的所有用户。

Switching Country I wish to show all users from that Country. 切换国家/地区我希望显示该国家/地区的所有用户。

There are also 2 kind of Users (Teachers and Students) and I wish to do the same in there as well. 还有2种用户(教师和学生),我也希望在那里也做同样的事情。

On the Teachers page I wish to show all teachers and based on the (previous or not) selected Country I wish to see Teachers from that Country... 我希望在“教师”页面上显示所有教师,并希望基于(先前(或未曾))所选国家/地区来查看该国家/地区的教师...

I'm having several issues doing this: 我这样做有几个问题:

1) The Homepage works fine, the homepage with /:countryCode works fine, /teachers and /students do not load anymore... If I put /:countryCode below everything the other pages load... (?!?). 1)主页运行良好,带有/:countryCode的主页运行正常, /teachers/students不再加载...如果我将/:countryCode放在其他页面的所有内容之下...(?!?)。

2) Do I also have to create routings like: /:countryCode/teachers ? 2)我还必须创建路由: /:countryCode/teachers吗? Or there is a way to store the country code somewhere...? 还是有一种方法可以将国家/地区代码存储在某处...?

3) In the Menu as well it seems like I have to create 2 different menu, one normal and one with country code extensions... 3)在菜单中也好像我必须创建2个不同的菜单,一个是普通菜单,另一个是带有国家/地区代码扩展名的菜单...

At the moment my routing is something like that: 目前,我的路由是这样的:

app.get('/', homeController.index);
app.get('/:countryCode', homeController.indexByCountry);
app.get('/teachers', userController.getTeachers);
app.get('/students', userController.getStudents);

I'm using the param :countryCode to query Users from that Country. 我正在使用param :countryCode查询来自该国家/地区的用户。

There is a better way to create all that? 有更好的方法来创建所有这些吗? Any Best Practice? 有最佳做法吗?

Online I don't see anything similar to this but I think it should be quite popular to have something like that. 在网上我没有看到类似的东西,但是我认为拥有这样的东西应该很受欢迎。

I hope you can help. 希望您能提供帮助。

From what I can figure from your question you're wanting to be able to handle when the country code is explicitly provided and a default case where you want to show all users (students, teachers or both) or use a previously provided country code. 从我可以从您的问题中看出,您希望能够在显式提供国家/地区代码时以及默认情况下希望显示所有用户(学生,教师或两者)或使用先前提供的国家/地区代码来处理。 Let me know if this is not your intention. 让我知道这是否不是您的意图。

Question 1: 问题1:

To answer your first question the reason that /teachers and /students will not load is that they are being matched as country codes. 要回答您的第一个问题, /teachers/students不会加载的原因是它们被匹配为国家/地区代码。 My understanding is that express will prioritise your routes in the order they are defined. 我的理解是,快速快递将按照定义的路线优先安排您的路线。

Sine /:countryCode is defined above the other two routes the url /teachers will invoke homeController.indexByCountry with the countryCode parameter being equal to "teachers" . 正弦/:countryCode定义在url /teachers将在countryCode参数等于"teachers"调用homeController.indexByCountry的其他两条路由之上。 By defining these routes above the /:countryCode route then they will have priority and work as intended. 通过在/:countryCode路由之上定义这些路由,它们将具有优先级并按预期工作。 So: 所以:

app.get('/', homeController.index); // Express will check this first
app.get('/teachers', userController.getTeachers); // Then this
app.get('/students', userController.getStudents); // Then this
app.get('/:countryCode', homeController.indexByCountry); // And finally this

Question 2: 问题2:

To indicate a new country code for teachers or students one solution could be to define routes like /:countryCode/teachers (or perhaps /teachers/:countryCode which makes more sense to me). 要为教师或学生指定新的国家/地区代码,一个解决方案是定义类似/:countryCode/teachers路线(或可能对我来说更有意义的/teachers/:countryCode )。

But if you want to save the country code you could use sessions . 但是,如果要保存国家/地区代码,则可以使用会话 There is an express middleware package called express-session which will help you do this. 有一个称为express-session的快速中间件软件包,可以帮助您完成此任务。

For example: 例如:

var express = require('express');
var session = require('express-session');

var app = express();

// Tell the express app to use the session middleware
app.use(session({
  secret: 'app secret key',
  resave: false,
  saveUninitialized: true
}));

app.get('/', function(req, res) {
  if (req.session.countryCode) { // If country code has been set
    res.send('Homepage, country code is currently ' + req.session.countryCode);
  } else {
    res.send('Homepage, country code not set');
  }
});

// Define routes for '/teachers' and '/students' etc.

app.get('/:countryCode', function(req, res) {
  // Set the countryCode property of the session object to the :countryCode
  // variable in the url
  req.session.countryCode = req.params.countryCode;

  res.send("Homepage, country code is now " + req.session.countryCode);
});

app.listen(3000);

Although one problem I encountered was that when I visited the server in my web browser, it would send a GET request to find the favicon and this was being interpreted as a country code and setting it to 'favicon.ico'. 尽管我遇到的一个问题是,当我在Web浏览器中访问服务器时,它会发送一个GET请求以找到该图标,这被解释为国家代码并将其设置为“ favicon.ico”。

One workaround to this may be checking that the country code provided meets some specific format before setting the session variable, for instance if you may want the country code always to be a 2 character string (Eg. "uk" or "fr"). 一种解决方法是,在设置会话变量之前检查提供的国家/地区代码是否符合某种特定格式,例如,如果您希望国家/地区代码始终为2个字符串(例如“ uk”或“ fr”)。

Question 3: 问题3:

Since we now know we can remember the country code which was previously set, you can just have a menu that links to /teachers etc. and you will still be able to use the country code from the session variable. 由于我们现在知道可以记住先前设置的国家/地区代码,因此您只需具有一个链接到/teachers等的菜单,您仍然可以使用会话变量中的国家/地区代码。

I hope this helps. 我希望这有帮助。

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

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