簡體   English   中英

我如何從 express ServerResponse [Autodesk forge] 的資源中獲取正文

[英]How can i get body from res of express ServerResponse [Autodesk forge]

看起來像ServerResponse [object]但我只想從中讀取 res.body.id 但無法 JSON.parse 或 JSON.stringify 因為那是

// 甚至不能讀取為 stream

function readStreamToEnd(stream) {
  var chunks = []; 
  stream.on('data', d => chunks.push(d));
  return new Promise((resolve, reject) => {
    stream.on('error', e => reject(e));
    stream.on('end', () => resolve(chunks[0] instanceof Buffer ? Buffer.concat(chunks) : chunks.join('')));
  }); 
}

res.on("finish",()=>{}) //getting issue in it too on route

路線https://imgur.com/5ZEuhT4

中間件

https://imgur.com/0thYNLq

Express.js 響應

https://imgur.com/LyQDFWt

循環引用 = <ref *1> stackoverflow

  console.log({res})
 //output
 {res: <ref *1> ServerResponse {}} //
ServerResponse {
   _events:
    [Object: null prototype] { finish: [ [Function: bound resOnFinish], [Function] ] },
   _eventsCount: 1,
   _maxListeners: undefined,
   output: [],
   outputEncodings: [],
   outputCallbacks: [],
   outputSize: 0,
   writable: true,
   _last: false,
   chunkedEncoding: false,
   shouldKeepAlive: true,
   useChunkedEncodingByDefault: true,
   sendDate: true,
   _removedConnection: false,
   _removedContLen: false,
   _removedTE: false,
   _contentLength: null,
   _hasBody: true,
   _trailer: '',
   finished: false,
   _headerSent: false,
   socket:
    Socket {
      connecting: false,
      _hadError: false,
      _handle:
       TCP {
         reading: true,
         onread: [Function: onStreamRead],
         onconnection: null,
         _consumed: true,
         [Symbol(owner)]: [Circular] },
      _parent: null,
      _host: null,
      _readableState:
       ReadableState {
         objectMode: false,
         highWaterMark: 16384,
         buffer: BufferList { head: null, tail: null, length: 0 },
         length: 0,
         pipes: null,
         pipesCount: 0,
         flowing: true,
         ended: false,
         endEmitted: false,
         reading: true,
         sync: false,
         needReadable: true,
         emittedReadable: false,
         readableListening: false,
         resumeScheduled: false,
         paused: false,
         emitClose: false,
         autoDestroy: false,
         destroyed: false,
         defaultEncoding: 'utf8',
         awaitDrain: 0,
         readingMore: false,
         decoder: null,
         encoding: null },
      readable: true,
      _events:
       [Object: null prototype] {
         end: [Array],
         drain: [Array],
         timeout: [Function: socketOnTimeout],
         data: [Function: bound socketOnData],
         error: [Function: socketOnError],
         close: [Array],
         resume: [Function: onSocketResume],
         pause: [Function: onSocketPause] },
      _eventsCount: 8,
      _maxListeners: undefined,
      _writableState:
       WritableState {
         objectMode: false,
         highWaterMark: 16384,
         finalCalled: false,
         needDrain: false,
         ending: false,
         ended: false,
         finished: false,
         destroyed: false,
         decodeStrings: false,
         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: false,
         autoDestroy: false,
         bufferedRequestCount: 0,
         corkedRequestsFree: [Object] },
      writable: true,
      allowHalfOpen: true,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server:
       Server {
         insecureHTTPParser: undefined,
         _events: [Object],
         _eventsCount: 5,
         _maxListeners: undefined,
         _connections: 6,
         _handle: [TCP],
         _usingWorkers: false,
         _workers: [],
         _unref: false,
         allowHalfOpen: true,
         pauseOnConnect: false,
         httpAllowHalfOpen: false,
         timeout: 120000,
         keepAliveTimeout: 5000,
         maxHeadersCount: null,
         headersTimeout: 40000,
         _connectionKey: '4:46.101.167.160:3000',
         [Symbol(IncomingMessage)]: [Function],
         [Symbol(ServerResponse)]: [Function],
         [Symbol(asyncId)]: 27 },
      _server:
       Server {
         insecureHTTPParser: undefined,
         _events: [Object],
         _eventsCount: 5,
         _maxListeners: undefined,
         _connections: 6,
         _handle: [TCP],
         _usingWorkers: false,
         _workers: [],
         _unref: false,
         allowHalfOpen: true,
         pauseOnConnect: false,
         httpAllowHalfOpen: false,
         timeout: 120000,
         keepAliveTimeout: 5000,
         maxHeadersCount: null,
         headersTimeout: 40000,
         _connectionKey: '4:46.101.167.160:3000',
         [Symbol(IncomingMessage)]: [Function],
         [Symbol(ServerResponse)]: [Function],
         [Symbol(asyncId)]: 27 },
      timeout: 120000,
      parser:
       HTTPParser {
         '0': [Function: parserOnHeaders],
         '1': [Function: parserOnHeadersComplete],
         '2': [Function: parserOnBody],
         '3': [Function: parserOnMessageComplete],
         '4': [Function: bound onParserExecute],
         _headers: [],
         _url: '',
         socket: [Circular],
         incoming: [IncomingMessage],
         outgoing: null,
         maxHeaderPairs: 2000,
         _consumed: true,
         onIncoming: [Function: bound parserOnIncoming],
         parsingHeadersStart: 1662484968545,
         [Symbol(isReused)]: true },
      on: [Function: socketOnWrap],
      _paused: false,
      _httpMessage: [Circular],
      [Symbol(asyncId)]: 1769,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]:
       Timeout {
         _called: false,
         _idleTimeout: 120000,
         _idlePrev: [TimersList],
         _idleNext: [TimersList],
         _idleStart: 73735,
         _onTimeout: [Function: bound ],
         _timerArgs: undefined,
         _repeat: null,
         _destroyed: false,
         [Symbol(unrefed)]: true,
         [Symbol(asyncId)]: 1770,
         [Symbol(triggerId)]: 1769 },
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0 },
   connection:
    Socket {
      connecting: false,
      _hadError: false,
      _handle:
       TCP {
         reading: true,
         onread: [Function: onStreamRead],
         onconnection: null,
         _consumed: true,
         [Symbol(owner)]: [Circular] },
      _parent: null,
      _host: null,
      _readableState:
       ReadableState {
         objectMode: false,
         highWaterMark: 16384,
         buffer: BufferList { head: null, tail: null, length: 0 },
         length: 0,
         pipes: null,
         pipesCount: 0,
         flowing: true,
         ended: false,
         endEmitted: false,
         reading: true,
         sync: false,
         needReadable: true,
         emittedReadable: false,
         readableListening: false,
         resumeScheduled: false,
         paused: false,
         emitClose: false,
         autoDestroy: false,
         destroyed: false,
         defaultEncoding: 'utf8',
         awaitDrain: 0,
         readingMore: false,
         decoder: null,
         encoding: null },
      readable: true,
      _events:
       [Object: null prototype] {
         end: [Array],
         drain: [Array],
         timeout: [Function: socketOnTimeout],
         data: [Function: bound socketOnData],
         error: [Function: socketOnError],
         close: [Array],
         resume: [Function: onSocketResume],
         pause: [Function: onSocketPause] },
      _eventsCount: 8,
      _maxListeners: undefined,
      _writableState:
       WritableState {
         objectMode: false,
         highWaterMark: 16384,
         finalCalled: false,
         needDrain: false,
         ending: false,
         ended: false,
         finished: false,
         destroyed: false,
         decodeStrings: false,
         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: false,
         autoDestroy: false,
         bufferedRequestCount: 0,
         corkedRequestsFree: [Object] },
      writable: true,
      allowHalfOpen: true,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server:
       Server {
         insecureHTTPParser: undefined,
         _events: [Object],
         _eventsCount: 5,
         _maxListeners: undefined,
         _connections: 6,
         _handle: [TCP],
         _usingWorkers: false,
         _workers: [],
         _unref: false,
         allowHalfOpen: true,
         pauseOnConnect: false,
         httpAllowHalfOpen: false,
         timeout: 120000,
         keepAliveTimeout: 5000,
         maxHeadersCount: null,
         headersTimeout: 40000,
         _connectionKey: '4:46.101.167.160:3000',
         [Symbol(IncomingMessage)]: [Function],
         [Symbol(ServerResponse)]: [Function],
         [Symbol(asyncId)]: 27 },
      _server:
       Server {
         insecureHTTPParser: undefined,
         _events: [Object],
         _eventsCount: 5,
         _maxListeners: undefined,
         _connections: 6,
         _handle: [TCP],
         _usingWorkers: false,
         _workers: [],
         _unref: false,
         allowHalfOpen: true,
         pauseOnConnect: false,
         httpAllowHalfOpen: false,
         timeout: 120000,
         keepAliveTimeout: 5000,
         maxHeadersCount: null,
         headersTimeout: 40000,
         _connectionKey: '4:46.101.167.160:3000',
         [Symbol(IncomingMessage)]: [Function],
         [Symbol(ServerResponse)]: [Function],
         [Symbol(asyncId)]: 27 },
      timeout: 120000,
      parser:
       HTTPParser {
         '0': [Function: parserOnHeaders],
         '1': [Function: parserOnHeadersComplete],
         '2': [Function: parserOnBody],
         '3': [Function: parserOnMessageComplete],
         '4': [Function: bound onParserExecute],
         _headers: [],
         _url: '',
         socket: [Circular],
         incoming: [IncomingMessage],
         outgoing: null,
         maxHeaderPairs: 2000,
         _consumed: true,
         onIncoming: [Function: bound parserOnIncoming],
         parsingHeadersStart: 1662484968545,
         [Symbol(isReused)]: true },
      on: [Function: socketOnWrap],
      _paused: false,
      _httpMessage: [Circular],
      [Symbol(asyncId)]: 1769,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]:
       Timeout {
         _called: false,
         _idleTimeout: 120000,
         _idlePrev: [TimersList],
         _idleNext: [TimersList],
         _idleStart: 73735,
         _onTimeout: [Function: bound ],
         _timerArgs: undefined,
         _repeat: null,
         _destroyed: false,
         [Symbol(unrefed)]: true,
         [Symbol(asyncId)]: 1770,
         [Symbol(triggerId)]: 1769 },
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0 },
   _header: null,
   _onPendingData: [Function: bound updateOutgoingData],
   _sent100: false,
   _expect_continue: false,
   req:
    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: false,
         needReadable: false,
         emittedReadable: false,
         readableListening: false,
         resumeScheduled: false,
         paused: false,
         emitClose: true,
         autoDestroy: false,
         destroyed: false,
         defaultEncoding: 'utf8',
         awaitDrain: 0,
         readingMore: false,
         decoder: null,
         encoding: null },
      readable: false,
      _events:
       [Object: null prototype] { end: [Function: resetHeadersTimeoutOnReqEnd] },
      _eventsCount: 1,
      _maxListeners: undefined,
      socket:
       Socket {
         connecting: false,
         _hadError: false,
         _handle: [TCP],
         _parent: null,
         _host: null,
         _readableState: [ReadableState],
         readable: true,
         _events: [Object],
         _eventsCount: 8,
         _maxListeners: undefined,
         _writableState: [WritableState],
         writable: true,
         allowHalfOpen: true,
         _sockname: null,
         _pendingData: null,
         _pendingEncoding: '',
         server: [Server],
         _server: [Server],
         timeout: 120000,
         parser: [HTTPParser],
         on: [Function: socketOnWrap],
         _paused: false,
         _httpMessage: [Circular],
         [Symbol(asyncId)]: 1769,
         [Symbol(lastWriteQueueSize)]: 0,
         [Symbol(timeout)]:
          Timeout {
            _called: false,
            _idleTimeout: 120000,
            _idlePrev: [TimersList],
            _idleNext: [TimersList],
            _idleStart: 73735,
            _onTimeout: [Function: bound ],
            _timerArgs: undefined,
            _repeat: null,
            _destroyed: false,
            [Symbol(unrefed)]: true,
            [Symbol(asyncId)]: 1770,
            [Symbol(triggerId)]: 1769 },
         [Symbol(kBytesRead)]: 0,
         [Symbol(kBytesWritten)]: 0 },
      connection:
       Socket {
         connecting: false,
         _hadError: false,
         _handle: [TCP],
         _parent: null,
         _host: null,
         _readableState: [ReadableState],
         readable: true,
         _events: [Object],
         _eventsCount: 8,
         _maxListeners: undefined,
         _writableState: [WritableState],
         writable: true,
         allowHalfOpen: true,
         _sockname: null,
         _pendingData: null,
         _pendingEncoding: '',
         server: [Server],
         _server: [Server],
         timeout: 120000,
         parser: [HTTPParser],
         on: [Function: socketOnWrap],
         _paused: false,
         _httpMessage: [Circular],
         [Symbol(asyncId)]: 1769,
         [Symbol(lastWriteQueueSize)]: 0,
         [Symbol(timeout)]:
          Timeout {
            _called: false,
            _idleTimeout: 120000,
            _idlePrev: [TimersList],
            _idleNext: [TimersList],
            _idleStart: 73735,
            _onTimeout: [Function: bound ],
            _timerArgs: undefined,
            _repeat: null,
            _destroyed: false,
            [Symbol(unrefed)]: true,
            [Symbol(asyncId)]: 1770,
            [Symbol(triggerId)]: 1769 },
         [Symbol(kBytesRead)]: 0,
         [Symbol(kBytesWritten)]: 0 },
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers:
       { connection: 'upgrade',
         host: 'URK',
         'content-length': '1822',
         'content-type': 'application/json; charset=utf-8' },
      rawHeaders:
       [ 'Connection',
         'upgrade',
         'Host',
         'URL',
         'Content-Length',
         '1822',
         'Content-Type',
         'application/json; charset=utf-8' ],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '/api/forge/datamanagement/signanddownloadpdf',
      method: 'POST',
      statusCode: null,
      statusMessage: null,
      client:
       Socket {
         connecting: false,
         _hadError: false,
         _handle: [TCP],
         _parent: null,
         _host: null,
         _readableState: [ReadableState],
         readable: true,
         _events: [Object],
         _eventsCount: 8,
         _maxListeners: undefined,
         _writableState: [WritableState],
         writable: true,
         allowHalfOpen: true,
         _sockname: null,
         _pendingData: null,
         _pendingEncoding: '',
         server: [Server],
         _server: [Server],
         timeout: 120000,
         parser: [HTTPParser],
         on: [Function: socketOnWrap],
         _paused: false,
         _httpMessage: [Circular],
         [Symbol(asyncId)]: 1769,
         [Symbol(lastWriteQueueSize)]: 0,
         [Symbol(timeout)]:
          Timeout {
            _called: false,
            _idleTimeout: 120000,
            _idlePrev: [TimersList],
            _idleNext: [TimersList],
            _idleStart: 73735,
            _onTimeout: [Function: bound ],
            _timerArgs: undefined,
            _repeat: null,
            _destroyed: false,
            [Symbol(unrefed)]: true,
            [Symbol(asyncId)]: 1770,
            [Symbol(triggerId)]: 1769 },
         [Symbol(kBytesRead)]: 0,
         [Symbol(kBytesWritten)]: 0 },
      _consuming: true,
      _dumped: false,
      next: [Function: next],
      baseUrl: '',
      originalUrl: '/api/forge/datamanagement/signanddownloadpdf',
      _parsedUrl:
       Url {
         protocol: null,
         slashes: null,
         auth: null,
         host: null,
         port: null,
         hostname: null,
         hash: null,
         search: null,
         query: null,
         pathname: '/api/forge/datamanagement/signanddownloadpdf',
         path: '/api/forge/datamanagement/signanddownloadpdf',
         href: '/api/forge/datamanagement/signanddownloadpdf',
         _raw: '/api/forge/datamanagement/signanddownloadpdf' },
      params: {},
      query: {},
      res: [Circular],
      body:
       { status: 'success',
         reportUrl:
          'URL from forge',
         stats: [Object],
         id: '42713e302eac4dc397b097e0bbaecd0d' },
      _body: true,
      length: undefined,
      route:
       Route {
         path: '/api/forge/datamanagement/signanddownloadpdf',
         stack: [Array],
         methods: [Object] } },
   locals: [Object: null prototype] {},
   write: [Function],
   [Symbol(isCorked)]: false,
   [Symbol(outHeadersKey)]:
    [Object: null prototype] { 'x-powered-by': [ 'X-Powered-By', 'Express' ] } }
``


  [1]: https://i.stack.imgur.com/9qjOi.png

JSON 不處理循環引用。

const circularJSON = require('circular-json');

app.post('/', async function (req, res) {
   let json = circularJSON.stringify(res); //can't able to do parse because 'ServerResponse prefix' so stringify
   let {body} = JSON.parse(json); // here parse will handle data 
   let id = body.id
   res.send(id)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM