POST表单路由在地址栏中有POST路由

时间:2017-02-21 05:56:15

标签: node.js mongodb express post

我正在使用node + express。我目前在表单中有一个文本输入,当您提交它时,会在具有给定搜索词的mongo DB中搜索用户。但是,每当我执行搜索时,URL应更改为“localhost:8080 / searchGlobalUsers”,它应为“localhost:8080 / talk”。这是我的邮寄路线:

app.post('/searchGlobalUsers', function(req, res){
    var regex = new RegExp(req.body.globalUserSearch, 'i');
    console.log("GLOBAL_USER_SEARCH: " + req.body.globalUserSearch);
    User.find({username: regex}, function(err, globalUserSearchQuery){
        res.render("talk", {globalUserSearchQuery : globalUserSearchQuery});
    });
});

这是我的文字输入+表格

   <form action="/searchGlobalUsers" method="POST" >
         <div class="row">
              <input type="text" name="globalUserSearch" class="u-full-width" placeholder="Search" id="add-friend-search-input">
         </div>
         <div class="row">
              <button class="button-green-large" class="u-full-width" id="add-friend-search-submit">Search</button>
         </div>
   </form>

1 个答案:

答案 0 :(得分:0)

渲染功能用于向客户端提供视图 只需使用此代码

    app.post('/searchGlobalUsers', function(req, res){
        var regex = new RegExp(req.body.globalUserSearch, 'i');
        console.log("GLOBAL_USER_SEARCH: " + req.body.globalUserSearch);
        User.find({username: regex}, function(err, globalUserSearchQuery){
            req.flash('info', globalUserSearchQuery);            
           return res.redirect("/talk");
        });
    });

   app.get('/talk', function(req, res){
     res.render("talk", {"globalUserSearchQuery" : req.flash('info')});
   })