javascript - res.redirect 不是 express 中的函数

标签 javascript node.js express

我目前正在尝试使用 Node 重定向到外部站点,并在 get 调用中专门表达。但是,我似乎无法找到可能的解决方案。任何帮助,将不胜感激。请注意,在尝试 response.redirect 时,我收到 TypeError: res.redirect is not a function。但是,当我查看 express 文档时,它似乎就在那里。

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

最佳答案

参数中的顺序很重要。 req 必须在前面,然后是 res,然后是 next。

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

关于javascript - res.redirect 不是 express 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43034673/

相关文章:

javascript - 将 CSS 定位从百分比转换为像素

node.js - Chartjs-node 安装失败

node.js - 对 Redis 数据库的异步 NodeJS 请求

javascript - 使用 Node 从 AWS CLI 获取区域

javascript - 调试或查找代码中的某些拼写错误的简单方法是什么?

javascript - 如何在 JavaScript 中仅使用条件语句编写硬币计数器函数?

javascript - 无法从 jQuery AJAX API 调用获取 JSON 数据

javascript - jQuery Cookie 在 IE 8 中不起作用?

node.js - index.js 文件中的 Azure Node.js 内部服务器错误

javascript - Node 中的基本 html 引擎