我希望搜索抛出数据库以获取用户在获取表单中输入的值。
后端非常简单,我知道如何搜索抛出数据库并呈现结果..
app.get('search/:id', function(req, res) {
var id = req.param("id");
mongoose.model('Something').find({
// the field that i want find. For example:
_id: id // This will search for the id field in the database, them will return a object if finds a match.
}, function(error, object){
if (error) {
res.send(error);
}
else {
res.send(object);
}
}
);
});
这个脚本可以工作。
现在我遇到了 HTML 问题。
我需要一个获取表单,将用户发送到/search/SOMETHING 用户想要搜索的内容,但我不知道如何动态更改 url..
非常欢迎任何帮助。
最佳答案
如果您不知道如何在 HTML 中实现所需的行为,您仍然可以在后端通过设置重定向来实现。表单提交后传入的请求将类似于 ?inputName=user search string
。
然后,您可以获取查询的值并对您的路线执行内部重定向
app.get('search', function(req, res) {
res.redirect('/search/' + req.query["inputName"]);
});
关于javascript - 如何创建一个简单的获取表单来创建动态路由,具体取决于搜索字段的值(MONGODB 和 Node.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27800362/