0

我是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);
    });

})
4

2 回答 2

0

在您的代码中,您执行了两次请求,一次是client.post通过调用client.methods.postMethod先前注册的。你应该做一个或另一个,response每个回调中的变量来自 http 客户端,它不是你的原始 json 响应。您的数据已经被解析并且它位于data变量中。

为了向您的休息服务器发送一个发布请求,您应该使用client.post或注册您的方法client.registerMethod,然后client.methods.registeredMethodName发送请求。

当您需要在代码中多次发送相同的发布请求时,请定义一个处理该请求响应的函数,例如:

function handleResponseA(data, response) {
    if(response.statusCode == 200){
        console.log(data);
    } else {
        switch(response.statusCode){
            case 404:
                console.log("Page not found.");
            break;

            case 500:
                console.log("Internal server error.");
            break;

            default:
                console.log("Response status code: " + response.statusCode);
        }
    }
}

然后对于client.post

client.post("http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", args, handleResponseA);
client.post("http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", args, handleResponseA);

并通过注册方法来完成它,通过以下方式注册方法:

client.registerMethod("postMethod", "http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", "POST");

然后调用注册方法:

client.methods.postMethod(args, handleResponseA);
client.methods.postMethod(args, handleResponseA);
于 2017-04-16T16:31:15.680 回答
0

你可以像这样使用axios

axios.post('/user', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
});
于 2017-04-16T12:08:03.107 回答