如何将查询参数添加到node.js / express.js res.redirect函数?

时间:2015-06-13 21:45:56

标签: javascript node.js express

我正在尝试将重定向用户用于我的应用中的Google授权页面。对于我的情况,我不能使用任何Google API模块。

我知道我可以格式化我的res.redirect()函数:

res.redirect('https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.readonly&response_type=code&redirect_uri=https%3A%2F%2Flocalhost%3A3000%2Fauth&client_id=690512789525-g8dbv0s1jo80u5hvevptqapeqokq7ees.apps.googleusercontent.com&hl=en&from_login=1&as=-6cc9a02870d13e22&authuser=0')

但我更喜欢更清洁的东西,我可以将查询参数保存在对象中。类似的东西:

res.redirect({
  uri: 'https://accounts.google.com/o/oauth2/auth,
  scope: 'https://............etc',
  response_type: 'code',
  [...etc...]
})

将我的每个查询参数作为对象中的属性值对。

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

认为您需要这样的内容url.format(urlObj)https://nodejs.org/docs/latest/api/url.html#url_url_format_urlobj