请求节点js中的疯狂

时间:2016-09-16 20:39:26

标签: node.js request

当我在节点中运行以下命令时:

request({
  "method": "POST",
  url: "http://localhost:8044/aut",
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({secret: "danzellovestodance"})
}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log("test"); console.log(response.body);
  }
});

我得到以下输出

{ domain: 
   { domain: null,
     _events: { error: [Function] },
     _maxListeners: undefined,
     members: [] },
  _events: { error: [Function], complete: [Function], pipe: [Function] },
  _maxListeners: undefined,
  method: 'POST',
  headers: 
   { 'Content-Type': 'application/json',
     host: 'localhost:8044',
     accept: 'application/json',
     'content-length': 31 },
  callback: [Function],
  readable: true,
  writable: true,
  explicitMethod: true,
  _qs: 
   { request: [Circular],
     lib: { stringify: [Function], parse: [Function] },
     useQuerystring: undefined,
     parseOptions: {},
     stringifyOptions: {} },
  _auth: 
   { request: [Circular],
     hasAuth: false,
     sentAuth: false,
     bearerToken: null,
     user: null,
     pass: null },
  _oauth: { request: [Circular], params: null },
  _multipart: 
   { request: [Circular],
     boundary: 'c3705445-78a6-45b2-80e3-418fffe560ae',
     chunked: false,
     body: null },
  _redirect: 
   { request: [Circular],
     followRedirect: true,
     followRedirects: true,
     followAllRedirects: false,
     allowRedirect: [Function],
     maxRedirects: 10,
     redirects: [],
     redirectsFollowed: 0,
     removeRefererHeader: false },
  _tunnel: 
   { request: [Circular],
     proxyHeaderWhiteList: 
      [ 'accept',
        'accept-charset',
        'accept-encoding',
        'accept-language',
        'accept-ranges',
        'cache-control',
        'content-encoding',
        'content-language',
        'content-location',
        'content-md5',
        'content-range',
        'content-type',
        'connection',
        'date',
        'expect',
        'max-forwards',
        'pragma',
        'referer',
        'te',
        'user-agent',
        'via' ],
     proxyHeaderExclusiveList: [] },
  setHeader: [Function],
  hasHeader: [Function],
  getHeader: [Function],
  removeHeader: [Function],
  localAddress: undefined,
  pool: {},
  dests: [],
  __isRequestRequest: true,
  _callback: [Function],
  uri: 
   { protocol: 'http:',
     slashes: true,
     auth: null,
     host: 'localhost:8044',
     port: '8044',
     hostname: 'localhost',
     hash: null,
     search: null,
     query: null,
     pathname: '/aut',
     path: '/aut',
     href: 'http://localhost:8044/aut' },
  proxy: null,
  tunnel: false,
  setHost: true,
  originalCookieHeader: undefined,
  _disableCookies: true,
  _jar: undefined,
  port: '8044',
  host: 'localhost',
  path: '/aut',
  _json: true,
  body: '{"secret":"daha"}',
  httpModule: 
   { IncomingMessage: { [Function: IncomingMessage] super_: [Object] },
     METHODS: 
      [ 'CHECKOUT',
        'CONNECT',
        'COPY',
        'DELETE',
        'GET',
        'HEAD',
        'LOCK',
        'M-SEARCH',
        'MERGE',
        'MKACTIVITY',
        'MKCOL',
        'MOVE',
        'NOTIFY',
        'OPTIONS',
        'PATCH',
        'POST',
        'PROPFIND',
        'PROPPATCH',
        'PURGE',
        'PUT',
        'REPORT',
        'SEARCH',
        'SUBSCRIBE',
        'TRACE',
        'UNLOCK',
        'UNSUBSCRIBE' ],
     OutgoingMessage: { [Function: OutgoingMessage] super_: [Object] },
     ServerResponse: { [Function: ServerResponse] super_: [Object] },
     STATUS_CODES: 
      { '100': 'Continue',
        '101': 'Switching Protocols',
        '102': 'Processing',
        '200': 'OK',
        '201': 'Created',
        '202': 'Accepted',
        '203': 'Non-Authoritative Information',
        '204': 'No Content',
        '205': 'Reset Content',
        '206': 'Partial Content',
        '207': 'Multi-Status',
        '300': 'Multiple Choices',
        '301': 'Moved Permanently',
        '302': 'Moved Temporarily',
        '303': 'See Other',
        '304': 'Not Modified',
        '305': 'Use Proxy',
        '307': 'Temporary Redirect',
        '308': 'Permanent Redirect',
        '400': 'Bad Request',
        '401': 'Unauthorized',
        '402': 'Payment Required',
        '403': 'Forbidden',
        '404': 'Not Found',
        '405': 'Method Not Allowed',
        '406': 'Not Acceptable',
        '407': 'Proxy Authentication Required',
        '408': 'Request Time-out',
        '409': 'Conflict',
        '410': 'Gone',
        '411': 'Length Required',
        '412': 'Precondition Failed',
        '413': 'Request Entity Too Large',
        '414': 'Request-URI Too Large',
        '415': 'Unsupported Media Type',
        '416': 'Requested Range Not Satisfiable',
        '417': 'Expectation Failed',
        '418': 'I\'m a teapot',
        '422': 'Unprocessable Entity',
        '423': 'Locked',
        '424': 'Failed Dependency',
        '425': 'Unordered Collection',
        '426': 'Upgrade Required',
        '428': 'Precondition Required',
        '429': 'Too Many Requests',
        '431': 'Request Header Fields Too Large',
        '500': 'Internal Server Error',
        '501': 'Not Implemented',
        '502': 'Bad Gateway',
        '503': 'Service Unavailable',
        '504': 'Gateway Time-out',
        '505': 'HTTP Version Not Supported',
        '506': 'Variant Also Negotiates',
        '507': 'Insufficient Storage',
        '509': 'Bandwidth Limit Exceeded',
        '510': 'Not Extended',
        '511': 'Network Authentication Required' },
     Agent: { [Function: Agent] super_: [Object], defaultMaxSockets: Infinity },
     globalAgent: 
      { domain: [Object],
        _events: [Object],
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: {},
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256 },
     ClientRequest: { [Function: ClientRequest] super_: [Object] },
     request: [Function],
     get: [Function],
     _connectionListener: [Function: connectionListener],
     Server: { [Function: Server] super_: [Object] },
     createServer: [Function],
     Client: [Function: deprecated],
     createClient: [Function: deprecated] },
  agentClass: 
   { [Function: Agent]
     super_: 
      { [Function: EventEmitter]
        EventEmitter: [Circular],
        usingDomains: true,
        defaultMaxListeners: 10,
        init: [Function],
        listenerCount: [Function] },
     defaultMaxSockets: Infinity },
  agent: 
   { domain: 
      { domain: null,
        _events: [Object],
        _maxListeners: undefined,
        members: [] },
     _events: { free: [Function] },
     _maxListeners: undefined,
     defaultPort: 80,
     protocol: 'http:',
     options: { path: null },
     requests: {},
     sockets: {},
     freeSockets: {},
     keepAliveMsecs: 1000,
     keepAlive: false,
     maxSockets: Infinity,
     maxFreeSockets: 256 } }
test
{ core: { 'test name': { disabled: false, flaky: false, bug: true } } }

如何静默调试输出?我只想得到:

test
    { core: { 'test name': { disabled: false, flaky: false, bug: true  } }

2 个答案:

答案 0 :(得分:0)

在交互模式下使用Node.js时(通过调用glReadPixels),最后一个语句的评估结果会自动打印到控制台:

node

在您的情况下,它是表示请求的对象。如果将该代码移动到JavaScript源文件并运行它(“node file.js”),则不再出现该输出。出于实验目的,假设您确实希望在交互模式下使用Node.js,只需使该语句返回其他内容即可。这方面有多种方法。您可以在代码末尾附加一个较短的值:

> 2+2;
4
> new Object();
{}
> 2+2; 0
0

或 将结果放在变量中:

request({
  "method": "POST",
  url: "http://localhost:8044/aut",
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({secret: "danzellovestodance"})
}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log("test"); console.log(response.body);
  }
}); 0 // will print 0, much cleaner

答案 1 :(得分:-1)

尝试:

var a= http.request({
 "method": "POST",
  url: "http://localhost:8044/aut",
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({secret: "danzellovestodance"})
}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log("test"); console.log(response.body);
   }
});