[英]res.redirect is not a function in express
我目前正在嘗試使用node重定向到外部站點,並專門在get調用中表示。 但是,我似乎找不到可能的解決方案。 任何幫助,將不勝感激。 請注意,當嘗試response.redirect時,我遇到TypeError:res.redirect不是一個函數。 但是,當我查看快速文檔時,它似乎在那里。
app.get('/:urlToForward', (res, req, next)=>{
//Stores the value of param
// var shorterUrl = res.params.urlToForward;
// shortUrl.findOne({'shorterUrl': shorterUrl}, (err,data)=>{
// // if (err) {
// // res.send("This shorterUurl does not exist.");
// // }
// // else {
// // res.redirect(301, data.originalUrl);
// // }
// // response.end();
// });
res.redirect('https://www.google.com');
});
您可以執行res.redirect('http://app.example.io');
Express文檔: http : //expressjs.com/api.html#res.redirect
只需使用簡單:
app is instance of invoked Express application.
app.get('/', function(request,respond) {
respond.redirect('your_url'); //Pass between the brackets your URL.
});
順序在參數中很重要。 req必須是第一個,然后是res,然后是下一個。
app.get('/:urlToForward', (req, res, next)=>{ ...
請注意,您可以將ES6速記用於shortUrl,而無需兩次鍵入。
app.get('/:urlToForward', (req, res, next)=> {
//Stores the value of param
var shorterUrl = res.params.urlToForward;
shortUrl.findOne({shorterUrl}, (err, data)=> {
if (err) {
res.send("This shorterUrl does not exist.");
}
else {
res.redirect(data.originalUrl);
}
response.end();
})
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.