繁体   English   中英

res.redirect不是express中的函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM