Node.js接受请求,形成2xx响应,但响应状态码为400

时间:2019-07-04 15:14:40

标签: c node.js http xmlhttprequest

Node.js接收POST请求并使用以下命令进行响应

router.post('/latest', function(req, res, next) {
    res.status(202).send(req.body);
    console.log(res);
});

观察console.log(res);的输出,服务器似乎制定了一个202...响应,但是_bytesDispatched等于28,对应于HTTP/1.1 400 Bad Request(实际响应I站在另一边)。为什么会这样?

console.log(res);的输出:

ServerResponse {
  _events: { finish: [Function: bound resOnFinish] },
  _eventsCount: 1,
  _maxListeners: undefined,
  output: 
   [ 'HTTP/1.1 202 Accepted\r\nX-Powered-By: Express\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: 7\r\nETag: W/"7-7mCT8n6gALm4N/SpGscZkTFIPTE"\r\nDate: Thu, 04 Jul 2019 14:59:08 GMT\r\nConnection: keep-alive\r\n\r\n',
     <Buffer 7b 22 62 22 3a 31 7d>,
     '' ],
  outputEncodings: [ 'latin1', undefined, 'latin1' ],
  outputCallbacks: [ null, null, [Function: bound onFinish] ],
  outputSize: 223,
  writable: true,
  _last: false,
  upgrading: false,
  chunkedEncoding: false,
  shouldKeepAlive: true,
  useChunkedEncodingByDefault: true,
  sendDate: true,
  _removedConnection: false,
  _removedContLen: false,
  _removedTE: false,
  _contentLength: 7,
  _hasBody: true,
  _trailer: '',
  finished: true,
  _headerSent: true,
  socket: 
   Socket {
     connecting: false,
     _hadError: false,
     _handle: 
      TCP {
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        onconnection: null,
        _consumed: true },
     _parent: null,
     _host: null,
     _readableState: 
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [BufferList],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     _events: 
      { finish: [Function: onSocketFinish],
        end: [Array],
        drain: [Array],
        timeout: [Function: socketOnTimeout],
        data: [Function: bound socketOnData],
        close: [Array],
        resume: [Function: onSocketResume],
        pause: [Function: onSocketPause],
        error: [Function] },
     _eventsCount: 9,
     _maxListeners: undefined,
     _writableState: 
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: true,
        ended: true,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        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: 1,
        prefinished: true,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     _bytesDispatched: 28,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     allowHalfOpen: true,
     server: 
      Server {
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        _connections: 1,
        _handle: [TCP],
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 120000,
        keepAliveTimeout: 5000,
        _pendingResponseData: 0,
        maxHeadersCount: null,
        _connectionKey: '6::::5761',
        [Symbol(IncomingMessage)]: [Function],
        [Symbol(ServerResponse)]: [Function],
        [Symbol(asyncId)]: 6 },
     _server: 
      Server {
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        _connections: 1,
        _handle: [TCP],
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 120000,
        keepAliveTimeout: 5000,
        _pendingResponseData: 0,
        maxHeadersCount: null,
        _connectionKey: '6::::5761',
        [Symbol(IncomingMessage)]: [Function],
        [Symbol(ServerResponse)]: [Function],
        [Symbol(asyncId)]: 6 },
     _idleTimeout: 120000,
     _idleNext: 
      TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _unrefed: true,
        msecs: 120000,
        nextTick: false,
        _timer: [Timer] },
     _idlePrev: 
      TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _unrefed: true,
        msecs: 120000,
        nextTick: false,
        _timer: [Timer] },
     _idleStart: 25909,
     _destroyed: false,
     parser: 
      HTTPParser {
        '0': [Function: parserOnHeaders],
        '1': [Function: parserOnHeadersComplete],
        '2': [Function: parserOnBody],
        '3': [Function: parserOnMessageComplete],
        '4': [Function: bound onParserExecute],
        _headers: [],
        _url: '',
        _consumed: true,
        socket: [Circular],
        incoming: [IncomingMessage],
        outgoing: null,
        maxHeaderPairs: 2000,
        onIncoming: [Function: bound parserOnIncoming] },
     on: [Function: socketOnWrap],
     _paused: false,
     _httpMessage: [Circular],
     [Symbol(asyncId)]: 32,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(bytesRead)]: 0,
     [Symbol(asyncId)]: 33,
     [Symbol(triggerAsyncId)]: 32 },
  connection: 
   Socket {
     connecting: false,
     _hadError: false,
     _handle: 
      TCP {
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        onconnection: null,
        _consumed: true },
     _parent: null,
     _host: null,
     _readableState: 
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [BufferList],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     _events: 
      { finish: [Function: onSocketFinish],
        end: [Array],
        drain: [Array],
        timeout: [Function: socketOnTimeout],
        data: [Function: bound socketOnData],
        close: [Array],
        resume: [Function: onSocketResume],
        pause: [Function: onSocketPause],
        error: [Function] },
     _eventsCount: 9,
     _maxListeners: undefined,
     _writableState: 
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: true,
        ended: true,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        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: 1,
        prefinished: true,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     _bytesDispatched: 28,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     allowHalfOpen: true,
     server: 
      Server {
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        _connections: 1,
        _handle: [TCP],
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 120000,
        keepAliveTimeout: 5000,
        _pendingResponseData: 0,
        maxHeadersCount: null,
        _connectionKey: '6::::5761',
        [Symbol(IncomingMessage)]: [Function],
        [Symbol(ServerResponse)]: [Function],
        [Symbol(asyncId)]: 6 },
     _server: 
      Server {
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        _connections: 1,
        _handle: [TCP],
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 120000,
        keepAliveTimeout: 5000,
        _pendingResponseData: 0,
        maxHeadersCount: null,
        _connectionKey: '6::::5761',
        [Symbol(IncomingMessage)]: [Function],
        [Symbol(ServerResponse)]: [Function],
        [Symbol(asyncId)]: 6 },
     _idleTimeout: 120000,
     _idleNext: 
      TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _unrefed: true,
        msecs: 120000,
        nextTick: false,
        _timer: [Timer] },
     _idlePrev: 
      TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _unrefed: true,
        msecs: 120000,
        nextTick: false,
        _timer: [Timer] },
     _idleStart: 25909,
     _destroyed: false,
     parser: 
      HTTPParser {
        '0': [Function: parserOnHeaders],
        '1': [Function: parserOnHeadersComplete],
        '2': [Function: parserOnBody],
        '3': [Function: parserOnMessageComplete],
        '4': [Function: bound onParserExecute],
        _headers: [],
        _url: '',
        _consumed: true,
        socket: [Circular],
        incoming: [IncomingMessage],
        outgoing: null,
        maxHeaderPairs: 2000,
        onIncoming: [Function: bound parserOnIncoming] },
     on: [Function: socketOnWrap],
     _paused: false,
     _httpMessage: [Circular],
     [Symbol(asyncId)]: 32,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(bytesRead)]: 0,
     [Symbol(asyncId)]: 33,
     [Symbol(triggerAsyncId)]: 32 },
  _header: 'HTTP/1.1 202 Accepted\r\nX-Powered-By: Express\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: 7\r\nETag: W/"7-7mCT8n6gALm4N/SpGscZkTFIPTE"\r\nDate: Thu, 04 Jul 2019 14:59:08 GMT\r\nConnection: keep-alive\r\n\r\n',
  _onPendingData: [Function: bound updateOutgoingData],
  _sent100: false,
  _expect_continue: false,
  req: 
   IncomingMessage {
     _readableState: 
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [BufferList],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: true,
        endEmitted: true,
        reading: false,
        sync: false,
        needReadable: false,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: false,
     _events: {},
     _eventsCount: 0,
     _maxListeners: undefined,
     socket: 
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [TCP],
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 9,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        _bytesDispatched: 28,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        allowHalfOpen: true,
        server: [Server],
        _server: [Server],
        _idleTimeout: 120000,
        _idleNext: [TimersList],
        _idlePrev: [TimersList],
        _idleStart: 25909,
        _destroyed: false,
        parser: [HTTPParser],
        on: [Function: socketOnWrap],
        _paused: false,
        _httpMessage: [Circular],
        [Symbol(asyncId)]: 32,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(bytesRead)]: 0,
        [Symbol(asyncId)]: 33,
        [Symbol(triggerAsyncId)]: 32 },
     connection: 
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [TCP],
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 9,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        _bytesDispatched: 28,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        allowHalfOpen: true,
        server: [Server],
        _server: [Server],
        _idleTimeout: 120000,
        _idleNext: [TimersList],
        _idlePrev: [TimersList],
        _idleStart: 25909,
        _destroyed: false,
        parser: [HTTPParser],
        on: [Function: socketOnWrap],
        _paused: false,
        _httpMessage: [Circular],
        [Symbol(asyncId)]: 32,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(bytesRead)]: 0,
        [Symbol(asyncId)]: 33,
        [Symbol(triggerAsyncId)]: 32 },
     httpVersionMajor: 1,
     httpVersionMinor: 1,
     httpVersion: '1.1',
     complete: true,
     headers: 
      { host: '127.0.0.1',
        'content-type': 'application/json; charset=utf-8',
        'content-length': '7' },
     rawHeaders: 
      [ 'Host',
        '127.0.0.1',
        'Content-Type',
        'application/json; charset=utf-8',
        'Content-Length',
        '7' ],
     trailers: {},
     rawTrailers: [],
     upgrade: false,
     url: '/latest',
     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: 9,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        _bytesDispatched: 28,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        allowHalfOpen: true,
        server: [Server],
        _server: [Server],
        _idleTimeout: 120000,
        _idleNext: [TimersList],
        _idlePrev: [TimersList],
        _idleStart: 25909,
        _destroyed: false,
        parser: [HTTPParser],
        on: [Function: socketOnWrap],
        _paused: false,
        _httpMessage: [Circular],
        [Symbol(asyncId)]: 32,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(bytesRead)]: 0,
        [Symbol(asyncId)]: 33,
        [Symbol(triggerAsyncId)]: 32 },
     _consuming: true,
     _dumped: false,
     next: [Function: next],
     baseUrl: '',
     originalUrl: '/latest',
     _parsedUrl: 
      Url {
        protocol: null,
        slashes: null,
        auth: null,
        host: null,
        port: null,
        hostname: null,
        hash: null,
        search: null,
        query: null,
        pathname: '/latest',
        path: '/latest',
        href: '/latest',
        _raw: '/latest' },
     params: {},
     query: {},
     res: [Circular],
     body: { b: 1 },
     _body: true,
     length: undefined,
     read: [Function],
     route: Route { path: '/latest', stack: [Array], methods: [Object] } },
  locals: {},
  statusCode: 202,
  statusMessage: 'Accepted',
  [Symbol(isCorked)]: false,
  [Symbol(outHeadersKey)]: 
   { 'x-powered-by': [ 'X-Powered-By', 'Express' ],
     'content-type': [ 'Content-Type', 'application/json; charset=utf-8' ],
     'content-length': [ 'Content-Length', '7' ],
     etag: [ 'ETag', 'W/"7-7mCT8n6gALm4N/SpGscZkTFIPTE"' ] } }

实际的传入请求(C)如下:

POST /latest HTTP/1.1
Host: 127.0.0.1
Content-Type: application/json; charset=utf-8
Content-Length: 7

{"b":1}

0 个答案:

没有答案
相关问题