我是nodeJS的初学者。
我正在尝试做的事情:
我托管了一个 POST API,当我使用邮递员调用它时,它会给出正确的响应,如下图所示:
但是,当我尝试使用 NodeJS node-rest-client (https://www.npmjs.com/package/node-rest-client)点击它时,它给了我一个不相关的大对象,如下所示:
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: true,
endEmitted: true,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [ [Function: responseOnEnd], [Function] ],
data: [Function],
error: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
socket:
Socket {
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
我需要帮助才能获得正确的对象,但我无法弄清楚我在这里做错了什么。
下面是我调用 REST API 的代码:
app.get('/notes', cors(), function(req, res) {
var args = {
"hiveMachineIp": "192.168.0.110",
"hiveMachinePort": "10000",
"hiveUsername": "nt",
"hivePassword": "",
"hiveDatabaseName": "default",
"hiveTableName": "transactions_24m",
"hiveAggregationColumn": "customerid",
"hiveAggregationFunction": "HISTOGRAM",
"hiveAggregationHistogramBin": "5"
};
var Client = require('node-rest-client').Client;
var client = new Client();
client.post("http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", args, function (data, response) {
// parsed response body as js object
// console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("postMethod", "http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", "POST");
client.methods.postMethod(args, function (data, response) {
// parsed response body as js object
// console.log(data);
// raw response
console.log(response);
});
})