V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
SJ2050cn
V2EX  ›  Node.js

如何在 nodejs 写的 api 接口中将上传的图片再 post 给另一个 api 接口

  •  
  •   SJ2050cn · 2020-02-12 15:05:06 +08:00 · 5212 次点击
    这是一个创建于 1506 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我写的 nodejs 接口是基于 koa 框架的,并使用 koa-better-body 将上传的文件解析,解析得到的文件数据如下:

    File {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      size: 297525,
      path: '/tmp/upload_0be4ede06f899420d94441f9d64ff49d',
      name: 'AIRBUS_HoF_2018_Screenshot1.jpeg',
      type: 'image/jpeg',
      hash: null,
      lastModifiedDate: 2020-02-12T06:32:46.177Z,
      _writeStream: WriteStream {
        _writableState: WritableState {
          objectMode: false,
          highWaterMark: 16384,
          finalCalled: true,
          needDrain: false,
          ending: true,
          ended: true,
          finished: true,
          destroyed: true,
          decodeStrings: true,
          defaultEncoding: 'utf8',
          length: 0,
          writing: false,
          corked: 0,
          sync: false,
          bufferProcessing: false,
          onwrite: [Function: bound onwrite],
          writecb: null,
          writelen: 0,
          bufferedRequest: null,
          lastBufferedRequest: null,
          pendingcb: 0,
          prefinished: true,
          errorEmitted: false,
          emitClose: false,
          autoDestroy: false,
          bufferedRequestCount: 0,
          corkedRequestsFree: [Object]
        },
        writable: false,
        _events: [Object: null prototype] {},
        _eventsCount: 0,
        _maxListeners: undefined,
        path: '/tmp/upload_0be4ede06f899420d94441f9d64ff49d',
        fd: null,
        flags: 'w',
        mode: 438,
        start: undefined,
        autoClose: true,
        pos: undefined,
        bytesWritten: 297525,
        closed: true
      }
    }
    

    然后如何将图片转发给另一个 api 接口呢(我是打算把图片上传至图床 api ),有没有办法将发送来的 file 数据流直接发个另一个 api,求大佬给个解决思路,只要说一下需要用的库和大概的思路就行,谢谢了

    5 条回复    2020-02-12 15:41:59 +08:00
    ilaipi
        1
    ilaipi  
       2020-02-12 15:17:52 +08:00   ❤️ 2
    应该是通过 http 请求调用 api,选一个发请求的库,推荐 axios

    https://github.com/axios/axios
    SJ2050cn
        2
    SJ2050cn  
    OP
       2020-02-12 15:30:40 +08:00 via Android
    @ilaipi 感谢回复,我想知道在 api 内部发送给其他域名下的 api 接口不就属于跨域了吗,怎么解决这个跨域问题呢
    fishlium
        3
    fishlium  
       2020-02-12 15:35:25 +08:00   ❤️ 1
    @SJ2050cn 跨域特指浏览器端
    justin2018
        4
    justin2018  
       2020-02-12 15:37:43 +08:00   ❤️ 1
    https://github.com/fhefh2018/upload2picbed

    可以用这个库 看 support picbed 这里 应该是楼主要的需求 ~
    SJ2050cn
        5
    SJ2050cn  
    OP
       2020-02-12 15:41:59 +08:00 via Android
    哦,那我去试试,谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5818 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 06:20 · PVG 14:20 · LAX 23:20 · JFK 02:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.