简体   繁体   English

无法使用express.js满足获取请求?

[英]Can't fulfill a get request using express.js?

Here is my server code: 这是我的服务器代码:

express
  .Router()
 .post('/authFB', async (req, res) => {
   // const agent = express();
    req.query = {
      client_id: 'ID',
      redirect_uri: `https://localhost:3000/`,
      client_secret: 'SECRET',
      code: Object.keys(req.body)[0]
    };

    req.baseUrl = `https://graph.facebook.com`

    const v = await req.get(`/v3.2/oauth/access_token`)
     v.body
  })

Problem I'm having is 'v' is always undefined - I've tested the facebook request via PostMan and it's working - it seems though my get request is never fulfilled. 我遇到的问题是'v'始终是不确定的-我已经通过PostMan测试了Facebook请求,并且该请求正在运行-似乎我的get请求从未得到满足。

I think that should be more like this 我认为应该更像这样

const got = require('got'); // I am using got for api call
express
    .Router()
    .post('/authFB', async (req, res) => {
        // const agent = express();
        const query = {
          client_id: 'ID',
          redirect_uri: `https://localhost:3000/`,
          client_secret: 'SECRET',
          code: Object.keys(req.body)[0]
        };
        const baseUrl = `https://graph.facebook.com`

        const v = await got(`${baseUrl}/v3.2/oauth/access_token`, { query });
        console.log(v.body);
        res.send(v.body);
    });

Here the data is fetched by 'got' and the response can be sent back to the client or use it in whatever way possible. 在这里,数据通过“获取”获取,并且响应可以发送回客户端或以任何可能的方式使用。

What you are doing here would be an antipattern because you are trying to edit the express request object which is not recommended unless used for authentication and other actions which are required for application level. 您在此处执行的操作将是反模式,因为您尝试编辑快速请求对象,除非用于身份验证和应用程序级别所需的其他操作,否则不建议使用该对象。

Also, that req is express.Request type. 同样,该请求是express.Request类型。 It doesn't have a get() property on it. 它没有get()属性。 Check this for your reference. 检查 ,供您参考。

Hope this explains your question well enough!!! 希望这足以解释您的问题!!! cheers :) 欢呼:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM