Dylan C. Israel
Dylan C. Israel

Reputation: 75

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');
});

Upvotes: 6

Views: 16273

Answers (4)

rcarterjr
rcarterjr

Reputation: 51

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

app.get('/:urlToForward', (req, res, next)=>{ ...

Upvotes: 5

Jonathan Eustace
Jonathan Eustace

Reputation: 2489

Note you can use ES6 shorthand for shorterUrl, no need to type it out twice.

 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();
        })
    });

Upvotes: 0

RyanM
RyanM

Reputation: 761

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

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

Upvotes: 3

Jakub Chlebowicz
Jakub Chlebowicz

Reputation: 529

Just use simple:

app is instance of invoked Express application.

    app.get('/', function(request,respond) {
      respond.redirect('your_url'); //Pass between the brackets your URL.
    });

Upvotes: 1

Related Questions