我到处搜索,但无法找到答案:。是否可以在node.js中的同一路径上添加多个路由器。喜欢: -
app.js
var users = require('./routes/users.js');
var users1 = require('./routes/users1.js');
app.use('/' , users);
app.use('/' , users1);
以上可能吗?!如果不是,如何修改上述内容?基本上我要做的是,第一个模块显示来自数据库的信息。当用户点击ADD按钮时,我的另一个模块被调用,负责获取新信息并将其存储在数据库中。
我对node.js完全不熟悉。任何帮助都会非常感激 谢谢!
degrees.ejs
<form id = 'degrees' method = POST action = '/main/degrees/add'>
<table id = "tg" class="tg" align = "center" style="width: 1041px">
<colgroup>
<col style="width: 80px">
<col style="width: 80px">
<col style="width: 100px">
<col style="width: 321px">
<col style="width: 425px">
<col style="width: 100px">
</colgroup>
<tr>
<th class="tg-yw4l" colspan="2"> Qualification <br/>(fill up like UG | B.TECH)<br><br></th>
<th class="tg-yw4l"> Field of Specialisation </th> </label>
<th class="tg-yw4l"> Institute </th> </label>
<th class="tg-yw4l"> Year of Passing </th> </label>
</tr>
<tr>
<td class="tg-yw4l1" style="width: 200px" contenteditable="true"> <%= Graduation %> </td>
<td class="tg-yw4l1" style="width: 200px" contenteditable="true"> <%= Degree %> </td>
<td class="tg-yw4l1" style="width: 321px" contenteditable="true"> <%= Specialisation %> </td>
<td class="tg-yw4l1" style="width: 525px" contenteditable="true"> <%= Institution %> </td>
<td class="tg-yw4l1" style="width: 250px" contenteditable="true"> <%= Year %> </td>
</tr>
<tr>
<td class="tg-yw4l2"><input type="text" name="Graduation" style="width: 90px"></td>
<td class="tg-yw4l2"><input type="text" name="Degree" style="width: 90px"></td>
<td class="tg-yw4l2"><input type="text" name="Specialisation" style="width: 321px"></td>
<td class="tg-yw4l2"><input type="text" name="Institution" style="width: 425px"></td>
<td class="tg-yw4l2"><input type="number" name="Years" style="width: 100px"></td>
</tr>
</table>
</form>
<button onclick = "add()"> + ADD </button>
</body>
<script type="text/javascript">
function add()
{
var tables = document.getElementById("tg");
var rows = tables.insertRow(-1);
var cell1 = rows.insertCell(0);
var cell2 = rows.insertCell(1);
var cell3 = rows.insertCell(2);
var cell4 = rows.insertCell(3);
var cell5 = rows.insertCell(4);
cell1.innerHTML = "<div contenteditable> </div>";
cell2.innerHTML = "<div contenteditable> </div>";
cell3.innerHTML = "<div contenteditable> </div>";
cell4.innerHTML = "<div contenteditable> </div>";
cell5.innerHTML = "<div contenteditable> </div>";
}
</script>
路由/ users.js
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://<username>:<password>@ds141434.mlab.com:41434/teacherspro';
var _db;
MongoClient.connect(url , function(err,db){
if (err) return;
_db = db;
});
router.post('/' , function (req,res){
_db.collection('project1').find({"UserName" : "karan"} , {Graduation : 1 , Degree : 1 , Specialisation : 1 , Institution : 1 , Year : 1 }).forEach(function(fifa){
console.log('loaded 2 time!');
res.render('degrees' , {Graduation : fifa.Graduation , Degree : fifa.Degree , Specialisation : fifa.Specialisation , Institution : fifa.Institution , Year : fifa.Year});
});
});
module.exports = router;
路由/ user1.js
var express = require('express');
var router = express.Router();
var MongoClient = require( 'mongodb' ).MongoClient;
var url = 'mongodb://<username>:<password>@ds141434.mlab.com:41434/teacherspro';
var _db;
MongoClient.connect(url , function(err,db){
if (err) return;
_db = db;
});
router.post('/' , function (req,res) {
res.render('degrees');
var users = {
Graduation : req.body.Graduation.Graduation,
Degree : req.body.Degree,
Specialisation : req.body.Specialisation,
Institution : req.body.Institution,
Year : parseInt(req.body.Year)
};
_db.collection('project1').insertOne({users : users} , function(err){
if(err) {console.log('no insert!');}
console.log('Data inserted!');
});
});
module.exports = router;
答案 0 :(得分:1)
允许,但匹配路由并发送响应而不调用next()
的第一个处理程序将具有优先权,并且不会调用其他路由处理程序。 Express按照声明的顺序匹配路由。匹配的第一个获得路线的第一个裂缝。如果它没有故意告诉Express通过调用next()
继续路由到其他处理程序,那么将不会调用其他路由处理程序。
如果您希望在调用路由处理程序后继续路由,则可以调用next()
告诉Express继续查找与该路由匹配的其他路由处理程序。
app.use('/' , users);
// then users could be this:
function users(req, res, next) {
if (want to handle route here) {
res.send(someResponse);
} else {
// tell Express to continue routing to other handlers
next();
}
}
所以我们假设我有1条路线从数据库中呈现我的信息..如果我点击ADD按钮,会出现一个新列,用于输入新信息,代码和详细信息在另一个模块中...如果我不知道怎么办?我想重定向,但再次使用相同的呈现页面来接收新信息?
如果您只想使用相同的按钮再次重复此过程,那就没问题了。将触发相同的路线并重复操作。
如果您想使用相同的路线来实现不同的操作,那么这不是一个合适的设计。给定路线应具有给定功能。如果要更改站点的其他功能的功能,请使用其他路径。您的表单帖子可以返回为下一个操作提供不同表单的新内容。您可以在页面中使用Javascript来更改未重新加载的页面中的逻辑。为了更具体地帮助解决问题的这个方面,我们必须看到实际的代码/ html才能知道发生了什么以及究竟推荐什么。但是,给定的路线不应该在不同的时间做两件不同的事情。这是一个错误的设计。
请记住,您还可以将查询参数与路由一起使用以将其他信息传递给路由处理程序,尽管使用查询参数来完全更改路由功能也是一个糟糕的设计。路径应该像带有参数的函数调用一样工作,并且每次调用它时通常应该做同样的事情(取决于你传递它的参数的含义)。