这很容易通过客户端路由器或客户端上的 JS 来完成。但由于 window 不是服务器上的对象,如何从服务器获取查询参数甚至读取 url?我检查了 Node 示例,但找不到任何与 Express js 无关的内容。
我的用例是 Instagram,它向我发送回需要在服务器上读取的代码,然后我从服务器发送带有该代码的 http 请求以检索访问 token 。
必须在页面加载,而不是加载然后通过客户端发送到服务器。
基本上我想检索“code”http://localhost:3000/?code=5e04c2e304f24f8b8380c2ec81202139的查询在服务器上。
最佳答案
我读了Instagram instruction for authentication ,看来您正在使用服务器端流程。我不知道你为什么选择这个而不是客户端身份验证流程,后者似乎更合适,我认为你应该重新考虑使用哪种方法。无论如何,要回答你的问题,你可以使用 WebApp
在 Meteor 中定义服务器路由的包:
WebApp.connectHandlers.use("/", function(req, res, next) {
if (req._parsedUrl.pathname === '/') {
const code = req.query.code;
console.log(code);
// get ACCESS_TOKEN with code
// if you need to redirect user after authorization
// res.writeHead(302, {
// Location: '/route/after/authorization'
// });
// res.end();
}
// comment this if you redirect to another route
next();
});
关于javascript - Meteor:从服务器上的url获取查询参数?用例 Instagram oAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41134203/