web-dev-qa-db-fra.com

Comment utiliser axios dans ExpressJS?

Je veux pouvoir utiliser mon application React pour faire une demande GET à mon serveur, ce qui est supposé inviter mon serveur à faire une demande GET à une API externe. J'utilise axios et j'ai essayé d'utiliser request, mais les deux me donnent ERRTIMEOUT. La requête fonctionne certainement comme j'ai essayé sur mon application frontale et les requêtes fonctionnent

const express = require("express");
const axios = require("axios");
const router = express.Router();

router.get("/test", (req, res, next) => {
    console.log("'/test' call");
    axios.get("https://api.neoscan.io/api/main_net/v1/get_all_nodes")
       .then(data => res.json(data))
       .catch(err => res.secn(err));
})

module.exports = router;`

Le code d'erreur

GGWP! { Error: connect ETIMEDOUT 104.25.167.105:443
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
  errno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  address: '104.25.167.105',
  port: 443,
  config:
   { adapter: [Function: httpAdapter],
     transformRequest: { '0': [Function: transformRequest] },
     transformResponse: { '0': [Function: transformResponse] },
     timeout: 0,
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus],
     headers:
      { Accept: 'application/json, text/plain, */*',
        'User-Agent': 'axios/0.18.0' },
     method: 'get',
     url: 'https://api.neoscan.io/api/main_net/v1/get_all_nodes',
     data: undefined },
  request:
   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,
        emitClose: true,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: true,
     _events:
      { response: [Function: handleResponse],
        error: [Function: handleRequestError] },
     _eventsCount: 2,
     _maxListeners: undefined,
     _options:
      { protocol: 'https:',
        maxRedirects: 21,
        maxBodyLength: 10485760,
        path: '/api/main_net/v1/get_all_nodes',
        method: 'get',
        headers: [Object],
        agent: undefined,
        auth: undefined,
        hostname: 'api.neoscan.io',
        port: null,
        nativeProtocols: [Object],
        pathname: '/api/main_net/v1/get_all_nodes' },
     _redirectCount: 0,
     _redirects: [],
     _requestBodyLength: 0,
     _requestBodyBuffers: [],
     _onNativeResponse: [Function],
     _currentRequest:
      ClientRequest {
        _events: [Object],
        _eventsCount: 6,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [TLSSocket],
        connection: [TLSSocket],
        _header:
         'GET /api/main_net/v1/get_all_nodes HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nUser-Agent: axios/0.18.0\r\nHost: api.neoscan.io\r\nConnection: close\r\n\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Agent],
        socketPath: undefined,
        timeout: undefined,
        method: 'GET',
        path: '/api/main_net/v1/get_all_nodes',
        _ended: false,
        res: null,
        aborted: undefined,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        _redirectable: [Circular],
        [Symbol(isCorked)]: false,
        [Symbol(outHeadersKey)]: [Object] },
     _currentUrl: 'https://api.neoscan.io/api/main_net/v1/get_all_nodes' },
  response: undefined }

Voici le code d'erreur supplémentaire que j'obtiens après avoir répondu

(node:35220) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at stringify (.\new-viewer\node_modules\express\lib\response.js:1119:12)
    at ServerResponse.json (.\new-viewer\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (.\new-viewer\node_modules\express\lib\response.js:158:21)
    at axios.get.then.catch.err (.\new-viewer\server\api\index.js:45:27)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:40496) UnhandledPromiseRejectionWarning: TypeError: res.error is not a function
    at axios.get.then.catch.err (.r\server\api\index.js:36:17)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:40496) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:40496) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
6
mLjH

Vous avez oublié de répondre au client:

 try{
    axios.get("https://api.neoscan.io/api/main_net/v1/get_all_nodes")
           .then(data => res.status(200).send(data))
           .catch(err => res.send(err));
 }
 catch(err){
    console.error("GG", err);
 }
2
abdulbarik
  1. Vous n'avez pas besoin de placer votre appel axios dans un try...catch car axios a déjà un bloc catch.

  2. Votre gestionnaire express doit renvoyer une réponse lorsque axios obtient une réponse de l'appel d'API ou qu'axios détecte une erreur lors de l'appel d'API.

Votre code devrait ressembler à ceci

router.get("/test", (req, res, next) => {
  console.log("'/test' call");
  axios.get("https://api.neoscan.io/api/main_net/v1/get_all_nodes")
    .then(data => res.json(data))
    .catch(err => next(err));
})

Si vous avez envie de async...await, vous pouvez écrire votre code comme ceci

router.get("/test", async (req, res, next) => {
  console.log("'/test' call");
  try {
    const res = await axios.get("https://api.neoscan.io/api/main_net/v1/get_all_nodes");
    res.json(data);
  }
  catch (err) {
    next(err)
  }
})
1
Dinesh Pandiyan

Axios vous renvoie l'intégralité de la réponse, donc si vous essayez de l'envoyer, vous obtenez une erreur de dépendance circulaire. Donc sur .then(data => res.json(data)), les données sont en fait la réponse.

Essayez .then(response => res.json(response.data))

0
Alex G