首页 > 解决方案 > 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);

标签: javascriptnode.jsexpressroutes

解决方案


您需要使用这样的查询参数:

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

推荐阅读