如何在 jade 中创建要在 node.js 中使用的变量
How to create a variable in jade to be used in node.js
我是express node新手,负责一个web app的前端。我需要能够让用户按下 link 并为此 link 按下时将变量传递给 index.js 此 link 让用户选择问题的类别他们想接受测试。我不认为为每个类别设置不同的路线是最佳选择。但是因为我是新手,所以我不知道该怎么做。
这是我在 jade 中的 link 的代码(我想在这里添加一些东西来做我想做的事)这个页面底部的屏幕截图有一个 link post.
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()});
})
}
在此先感谢您的帮助!
here is a screenshot of the web app with the category choices
所以看起来您正在尝试做的是让一个按钮将数据发送回您的服务器。这样做需要向服务器发出另一个请求。您可以添加一个路由来处理这些数据......
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);
);
我是express node新手,负责一个web app的前端。我需要能够让用户按下 link 并为此 link 按下时将变量传递给 index.js 此 link 让用户选择问题的类别他们想接受测试。我不认为为每个类别设置不同的路线是最佳选择。但是因为我是新手,所以我不知道该怎么做。
这是我在 jade 中的 link 的代码(我想在这里添加一些东西来做我想做的事)这个页面底部的屏幕截图有一个 link post.
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()});
})
}
在此先感谢您的帮助!
here is a screenshot of the web app with the category choices
所以看起来您正在尝试做的是让一个按钮将数据发送回您的服务器。这样做需要向服务器发出另一个请求。您可以添加一个路由来处理这些数据......
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);
);