我是表达节点的新手,我负责Web应用程序的前端。我需要能够让用户按下一个链接,当按下将变量传递给index.js时,该链接将指向该链接,以便用户选择他们要测试的问题类别。我不认为为每个类别选择不同的路线是最好的选择。但是因为我是新来的,所以我不确定该怎么做。
这是我在jade中链接的代码(我想在这里添加一些东西来做我想做的事情),在此文章底部有一个指向该页面的屏幕截图的链接。
li.active
a(href='freeplay')
i.fa.fa-edit
span.nav-label Freeplay
span.fa.arrow
ul.nav.nav-second-level
li
a(href='freeplay/category') Category1
li
a(href='freeplay/category') Category2
li
a(href='freeplay/category') Category3
li
a(href='freeplay/category') Category4
li
a(href='freeplay/category') Category5
这是我处理它的index.js。temp是我要保存类别字符串的变量。
//Handle the free play request
router.get('/freeplay' , isAuthenticated, freeplay.startFreePlay);
//Handle the free play request with category
router.get('/freeplay/category' , isAuthenticated, freeplay.startCategoryPlay);
最后,我希望能够将变量读取到temp的node.js是我要将用户选择的类别分配给的变量。
exports.startFreePlay = function(req, res) {
//grab a question that the user has not gotten right
//Get the users logged in id
userId = req.session.passport.user;
freePlayLogic.getQuestion(userId, function(question){
res.render("FreePlayQuestion", { question: question , questionID : question._id.toString()});
})
//Pass the question to render
};
exports.startCategoryPlay = function(req, res){
//grab a question that the user has not gotten right
//Get the users logged in id
userId = req.session.passport.user;
/**
* get a question from category "temp"
*/
freePlayLogic.getQuestionFromCategory(userId, "temp", function(question){
res.render("FreePlayQuestion", { question: question , questionID : question._id.toString()});
})
}
在此先感谢您的帮助!
因此,您要尝试执行的操作是有一个将数据发送回服务器的按钮。这样做需要向服务器发出另一个请求。您可以添加一条路线来处理这些数据,如下所示...
app.get('/question/:id', function(req, res) {
var id = req.params.id;
//do something with this id like render a page
);
然后您的链接看起来像这样
a(href="/question/1") Question 1
a(href="/question/2") Question 2
这应该可行,如果您有任何问题,请发表评论:)
尝试添加此测试路线
app.get('/test/id', function(req, res) {
res.send(req.params.id);
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句