javascript - NodeJS:如何重定向到外部 URL?
问题描述
我有一个/auth
接受请求的 GET 路由,需要重定向到外部资源(比如说https://google.com
),但不是将我重定向到https://google.com
它,而是将我重定向到http:localhost:3000/api/auth/https://google.com
.
是否有可能以某种方式重定向到外部资源?https://google.com
因为现在它在我的路线中搜索路径。
这是我的路由器:
export const loginController = async (req: Request, res: Response, next: NextFunction) => {
res.redirect('https://google.com');
};
router.get('/login', loginController);
解决方案
您需要使用这样的查询参数:
http:localhost:3000/api/auth?redirectUrl=https://google.com
export const loginController = async (req: Request, res: Response, next: NextFunction) => {
const { redirectUrl } = req.query
res.redirect(redirectUrl);
};
推荐阅读
- android - 使用已存在的电子邮件或用户名保存用户会导致 ParseUser 返回错误的值
- arrays - 如何将字符串数组作为函数自己的参数传递?编译器说它不起作用
- python - 是否想通过比较 pandas 数据框中的另一列来为新列赋值?
- docker - how to remove all the build history of a docker image?
- c++ - 对随机序列进行下采样会使其随机性降低吗?有没有一个原理/定理可以证明这一点?
- python - 将数据写入文件时导致问题的附加行字符
- python - 在这种情况下如何从正则表达式中提取数据?
- ios - 无法使用自动布局将简单的自行调整大小的集合视图单元格调整为标签大小
- php - 找不到 php 文件的文件
- c - 为什么 a = 6 在海量运算中不将 6 分配给 a?(C 编程)