在使用axios POST请求发送数据以及在另一个微服务中获取/获取数据时需要帮助

时间:2019-11-24 09:31:54

标签: node.js axios microservices koa

我正在尝试使用axios post方法将数据发送到具有不同端口号的其他微服务。但是在其他微服务中,im无法获取/获取从服务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] }

1 个答案:

答案 0 :(得分:0)

您在使用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(),....

我希望能有所帮助。