2016-10-31 22 views
0

我有一個表單,我將信息插入到數據庫中。那部分工作得很好,但是如何在那之後重定向到另一個頁面呢?將數據發佈到我的數據庫後,如何重定向到另一個頁面?

app.post('/create', function (req, res) { 
 
     Room.findOne({'name' :req.body.name}, function(err,room){ 
 
      if(err){ 
 
       return done(err); 
 
      } 
 

 
      if(room){ 
 
       return done(null,false,req.flash('this room name is taken')); 
 
      }else{ 
 
       var newRoom = new Room(); 
 
       newRoom.name = req.body.name; 
 
       newRoom.topic = req.body.topic; 
 
       newRoom.participants = req.body.participants; 
 
       newRoom.type = req.body.type; 
 
      } 
 
      newRoom.save(function(err){ 
 
       if (err){ 
 
        throw err; 
 
       } 
 
       redirect: '/home'; 
 
      })

回答

1

參考here審查http.Response API進行的NodeJS。 response.writeHead(的StatusCode [,statusMessage] [,標頭])

與用於重定向以下

res.writeHead(302,{ 
      'Location':'/path', 
     }); 
     res.end(); 

狀態碼替換該行

redirect: '/home'; 

是3XX水平,在302該示例用於'找到' '位置'標題將給出重定向到的路徑

如果使用Express編寫,請參閱ENCE here和使用

res.redirect([statusCode,] '/path') 

如果可選狀態代碼沒有明確表示,這將是302「發現」默認

2

隨着快速可以使用res.redirect()方法。有關完整文檔click here

在你的情況下,更換:

redirect: '/home'; 

下列要求:

res.redirect(301, '/home'); 
0
res.redirect(pathnam) 

默認情況下它會發送302個狀態碼, 或者您也可以通過狀態碼第一個參數

res.redirect(301, pathname) 
相關問題