【问题标题】:need help in sending data using axios POST request and getting/fetching the data in another microservices在使用 axios POST 请求发送数据以及在另一个微服务中获取/获取数据方面需要帮助
【发布时间】:2019-11-25 11:30:29
【问题描述】:

我正在尝试使用 axios post 方法将数据发送到具有不同端口号的不同微服务。但是在其他微服务中,我无法获取/获取从服务 A 发送到服务 B 的数据。如何在另一个微服务中获取 firstnamelastname 的值?我已经安装了body-parser,但仍然无法在微服务之间通信/发送数据的最佳方法是什么?谢谢

Service A (Port 3001)

     axios.post('http://localhost:3000/v1/wsData', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
});





Service B (Port 3000)


router.post('/v1/wsData',async(ctx, next) => { 
  try {
    const result =  ctx.response.firstName
    console.log(result)
    await next ()
  } catch(err) {
    throw err
  }
}
)

多次尝试后,我不断收到错误,例如数据未定义、没有对象等

response: 
   { status: 404,
     statusText: 'Not Found',
     headers: 
      { vary: 'Accept-Encoding, Origin',
        'content-type': 'text/plain; charset=utf-8',
        'content-length': '9',
        date: 'Sun, 24 Nov 2019 09:15:38 GMT',
        connection: 'close' },
     config: 
      { url: 'http://localhost:3000/v1/wsData',
        method: 'post',
        data: '{"firstName":"Fred","lastName":"Flintstone"}',
        headers: [Object],
        transformRequest: [Array],
        transformResponse: [Array],
        timeout: 0,
        adapter: [Function: httpAdapter],
        xsrfCookieName: 'XSRF-TOKEN',
        xsrfHeaderName: 'X-XSRF-TOKEN',
        maxContentLength: -1,
        validateStatus: [Function: validateStatus] },
     request: 
      ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 6,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        upgrading: false,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: true,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: null,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Object],
        connection: [Object],
        _header: 'POST /v1/wsData HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/json;charset=utf-8\r\nUser-Agent: axios/0.19.0\r\nContent-Length: 44\r\nHost: localhost:3000\r\nConnection: close\r\n\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Object],
        socketPath: undefined,
        timeout: undefined,
        method: 'POST',
        path: '/v1/wsData',
        _ended: true,
        res: [Object],
        aborted: undefined,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        _redirectable: [Object],
        [Symbol(outHeadersKey)]: [Object] },
     data: 'Not Found' },
  isAxiosError: true,
  toJSON: [Function] }

【问题讨论】:

    标签: node.js axios microservices koa


    【解决方案1】:

    你在用 expressjs 吗?

    如果是这样,您可以使用 app.use(express.json()) 或/和 app.use(express.urlencoded({extended: boolean}) 作为正文解析中间件。

    ctx 是什么意思?

    router.post('/v1/wsData', (req, res, next) => {
        //you can access the body with req.body.
    });
    

    您是否在下一个中间件中发送响应?如果没有,您可以使用 res.send、res.json()、res.sendStatus()、...。

    希望能帮上点忙。

    【讨论】:

    • 嗨,我正在使用 koajs,我正在使用 koa body-parser 应用程序,我尝试过使用解析器不工作:( .use(errorHandler) .use(headerHandler) .use(bodyParser({ urlencoded: '56kb', jsonLimit: '1mb' }))
    猜你喜欢
    • 2021-12-17
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多