Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

res.redirect is not a function in express

I'm currently trying to redirect to an external site with node and express specifically in a get call. However, I can't seem to find a possible solution. Any help would be appreciated. Note that when trying response.redirect I'm getting TypeError: res.redirect is not a function. However, when I view the express documentation it seems to be in there.

    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');
});
like image 689
Dylan C. Israel Avatar asked Mar 26 '17 21:03

Dylan C. Israel


2 Answers

Order matters in the arguments. req must be first, then res, then next.

app.get('/:urlToForward', (req, res, next)=>{ ...
like image 76
rcarterjr Avatar answered Oct 26 '22 01:10

rcarterjr


You can do res.redirect('http://app.example.io');

Express docs: http://expressjs.com/api.html#res.redirect

like image 30
RyanM Avatar answered Oct 26 '22 03:10

RyanM