Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Expressjs动态路由问题_Javascript_Node.js_Express_Routing - Fatal编程技术网

Javascript Expressjs动态路由问题

Javascript Expressjs动态路由问题,javascript,node.js,express,routing,Javascript,Node.js,Express,Routing,我正在使用Express4.0,我正在尝试创建一个路由,在主页中显示来自所有国家/地区的所有用户 切换国家/地区我希望显示来自该国家/地区的所有用户 还有两种用户(教师和学生),我也希望在那里做同样的事情 在教师页面上,我希望显示所有教师,并根据(以前或没有)选择的国家,我希望看到来自该国的教师 我在这方面遇到了几个问题: 1) 主页正常运行,带有/:countryCode的主页正常运行,/teachers和/student不再加载。。。如果我把/:countryCode放在其他页面加载的所有内

我正在使用Express4.0,我正在尝试创建一个路由,在主页中显示来自所有国家/地区的所有用户

切换国家/地区我希望显示来自该国家/地区的所有用户

还有两种用户(教师和学生),我也希望在那里做同样的事情

在教师页面上,我希望显示所有教师,并根据(以前或没有)选择的国家,我希望看到来自该国的教师

我在这方面遇到了几个问题:

1) 主页正常运行,带有
/:countryCode
的主页正常运行,
/teachers
/student
不再加载。。。如果我把
/:countryCode
放在其他页面加载的所有内容下面。。。(?!?)

2) 我是否也必须创建诸如:
/:countryCode/teachers
之类的路线?或者有一种方法可以将国家代码存储在某处

3) 在菜单以及它似乎我必须创建两个不同的菜单,一个正常和一个国家代码扩展

目前我的路线是这样的:

app.get('/', homeController.index);
app.get('/:countryCode', homeController.indexByCountry);
app.get('/teachers', userController.getTeachers);
app.get('/students', userController.getStudents);
我正在使用param
:countryCode
查询来自该国家的用户

有更好的方法来创造这一切吗?有什么最佳做法吗

在网上我没有看到任何类似的东西,但我认为这样的东西应该很流行


希望您能提供帮助。

从您的问题中我可以看出,当明确提供国家代码时,您希望能够处理国家代码,以及您希望显示所有用户(学生、教师或两者)或使用以前提供的国家代码的默认情况。如果这不是你的意图,请告诉我

问题1: 要回答您的第一个问题,
/teachers
/student
不会加载的原因是它们被匹配为国家代码。我的理解是,express将按照定义的顺序优先安排您的路线


Sine
/:countryCode
定义在其他两条路由之上,url
/teachers
将调用
homeController.indexByCountry
,其中
countryCode
参数等于
“teachers”
。通过在
/:countryCode
路线上方定义这些路线,它们将具有优先级并按预期工作。因此:

问题2: 为教师或学生指定新的国家代码,一种解决方案是定义路径,如
/:countryCode/teachers
(或者可能是
/teachers/:countryCode
,这对我来说更有意义)

但是如果您想保存国家代码,可以使用会话。有一个名为的express中间件包可以帮助您实现这一点

例如:

虽然我遇到的一个问题是,当我在我的web浏览器中访问服务器时,它会发送一个GET请求来查找favicon,这被解释为国家代码并将其设置为“favicon.ico”

解决方法之一是在设置会话变量之前检查提供的国家代码是否符合某些特定格式,例如,如果您希望国家代码始终为2个字符的字符串(例如“uk”或“fr”)

问题3: 由于我们现在知道我们可以记住以前设置的国家代码,因此您只需拥有一个链接到
/teachers
等的菜单,就可以使用会话变量中的国家代码


我希望这能有所帮助。

/:countryCode
是一个变量,它将匹配任何以斜杠开头并以字符串结尾的内容,例如
/students
/canada
等。如果您需要更多嵌套,请执行
/:countryCode/:type
etcok,但我应该如何按国家查询
/uk/teachers
/fr/teachers
或所有教师:
/teachers
?只需为
/:countryCode/teachers
添加一条路径,但在
/teachers
之后添加它即可
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
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);