我正在尝试使用axios post方法将数据发送到具有不同端口号的其他微服务。但是在其他微服务中,im无法获取/获取从服务A发送到服务B的数据。我如何在另一个微服务中获取firstname
和lastname
的值?我已经安装了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] }