R
R
Ruslan Absalyamov2018-08-22 13:54:19
Node.js
Ruslan Absalyamov, 2018-08-22 13:54:19

Why is my Not Found page popping up?

I keep getting the Not Found error.

const Koa = require('koa'),
    Client = require('node-rest-client').Client;

const app = new Koa();
const client = new Client();
const url = 'https://www.binance.com/api/v1/klines?interval=1h&limit=24&symbol=BNBBTC';

app.use(async ctx => {
    await client.get(url, (data, response) => {
        ctx.body = JSON.stringify(data);
    });
});

app.listen(3002);

As if data in the console displays data for me, but not on the page
[ [ 1534849200000,
    '0.00151270',
    '0.00151480',
    '0.00150620',
    '0.00150950',
    '12238.78000000',
    1534852799999,
    '18.49577488',
    1045,
    '7293.00000000',
    '11.02999493',
    '0' ],
...
]

If you output what this handler itself displays
let dataParse = client.get(url, (data, response) => {
    return data;
});
console.log(dataParse);

Then he displays the entire response, but how
ClientRequest {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  href: 'https://www.binance.com/api/v1/klines?interval=1h&limit=24&symbol=BNBBTC',
  options: 
   { host: 'www.binance.com',
     port: null,
     path: '/api/v1/klines?interval=1h&limit=24&symbol=BNBBTC',
     href: 'https://www.binance.com/api/v1/klines?interval=1h&limit=24&symbol=BNBBTC',
     method: 'GET',
     headers: {} },
  _httpRequest: 
   Writable {
     _writableState: 
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        destroyed: false,
        decodeStrings: true,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: true,
     domain: null,
.....

Then how to display what is in data?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2018-08-22
@rusline18

` client.get` does not return a promise, so await doesn't work there.
We need to look at the dock and make sure that the promise is returned, which needs to be awaited.
It is inappropriate to confuse callback and await approaches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question