Поиск по блогу

среда, 27 мая 2015 г.

Пробуем команды debugger; чтобы исследовать модуль request nodejs

Чтобы вывести в консоль заголовки можно использовать NODE_DEBUG=request node r01.js . Команда выглядит необычно, второй вариант NODE_DEBUG=response ... Эта опция дожна рвботать с любым модулем (?).

Сначала я хотел попробовать здесь разные варианты опций

HTTP Headers, such as User-Agent, can be set in the options object. In the example below, we call the github API to find out the number of stars and forks for the request repository. This requires a custom User-Agent header as well as https.

In [ ]:
var request = require('request');

var options = {
  url: 'https://api.github.com/repos/request/request',
  headers: {
    'User-Agent': 'request'
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    var info = JSON.parse(body);
    console.log(info.stargazers_count + " Stars");
    console.log(info.forks_count + " Forks");
  }
}

request(options, callback);

Debugging

There are at least three ways to debug the operation of request:

Launch the node process like NODE_DEBUG=request node script.js (lib,request,otherlib works too).

Set require('request').debug = true at any time (this does the same thing as #1).

Use the request-debug module to view request and response headers and bodies.
In [ ]:
kiss@kali:~/Desktop/scr$ NODE_DEBUG=request node r01.js 
REQUEST { uri: 'http://www.reddit.com', callback: [Function] }
REQUEST make request http://www.reddit.com/
REQUEST onRequestResponse http://www.reddit.com/ 200 { date: 'Wed, 27 May 2015 13:04:28 GMT',
  'content-type': 'text/html; charset=UTF-8',
  'transfer-encoding': 'chunked',
  connection: 'keep-alive',
  'set-cookie': [ '__cfduid=d737e5b2b517478f7275d67a388d3fa851432731868; expires=Thu, 26-May-16 13:04:28 GMT; path=/; domain=.reddit.com; HttpOnly' ],
  'x-ua-compatible': 'IE=edge',
  'x-frame-options': 'SAMEORIGIN',
  'x-content-type-options': 'nosniff',
  'x-xss-protection': '1; mode=block',
  vary: 'accept-encoding',
  'cache-control': 'no-cache',
  'x-moose': 'majestic',
  'cf-cache-status': 'HIT',
  server: 'cloudflare-nginx',
  'cf-ray': '1ed1ecff4496168e-ARN' }
REQUEST finish init function http://www.reddit.com/
REQUEST response end http://www.reddit.com/ 200 { date: 'Wed, 27 May 2015 13:04:28 GMT',
  'content-type': 'text/html; charset=UTF-8',
  'transfer-encoding': 'chunked',
  connection: 'keep-alive',
  'set-cookie': [ '__cfduid=d737e5b2b517478f7275d67a388d3fa851432731868; expires=Thu, 26-May-16 13:04:28 GMT; path=/; domain=.reddit.com; HttpOnly' ],
  'x-ua-compatible': 'IE=edge',
  'x-frame-options': 'SAMEORIGIN',
  'x-content-type-options': 'nosniff',
  'x-xss-protection': '1; mode=block',
  vary: 'accept-encoding',
  'cache-control': 'no-cache',
  'x-moose': 'majestic',
  'cf-cache-status': 'HIT',
  server: 'cloudflare-nginx',
  'cf-ray': '1ed1ecff4496168e-ARN' }
REQUEST end event http://www.reddit.com/
REQUEST has body http://www.reddit.com/ 113303
REQUEST emitting complete http://www.reddit.com/

Приложения. Примеры из консоли repl после запроса request('http://www.reddit.com', function ...});

In [ ]:
> response.
response.__defineGetter__      response.__defineSetter__      response.__lookupGetter__
response.__lookupSetter__      response.constructor           response.hasOwnProperty
response.isPrototypeOf         response.propertyIsEnumerable  response.toLocaleString
response.toString              response.valueOf               

response._consuming            response._dumped               response._events
response._maxListeners         response._pendingIndex         response._pendings
response._readableState        response.addListener           response.body
response.caseless              response.client                response.complete
response.connection            response.domain                response.headers
response.httpVersion           response.httpVersionMajor      response.httpVersionMinor
response.method                response.on                    response.pause
response.pipe                  response.read                  response.readable
response.req                   response.request               response.resume
response.socket                response.statusCode            response.toJSON
response.trailers              response.upgrade               response.url
In [ ]:
> response.headers
{ 'cf-ray': '1ec8d56b6911059d-ARN',
  vary: 'accept-encoding',
  server: 'cloudflare-nginx',
  'x-xss-protection': '1; mode=block',
  'x-frame-options': 'SAMEORIGIN',
  date: 'Tue, 26 May 2015 10:35:30 GMT',
  connection: 'keep-alive',
  'cf-cache-status': 'HIT',
  'transfer-encoding': 'chunked',
  'set-cookie': [ '__cfduid=d6837b3269777bbc6c3eff42eb862b2bc1432636530; expires=Wed, 25-May-16 10:... (length: 127)' ],
  'x-ua-compatible': 'IE=edge',
  'x-content-type-options': 'nosniff',
  'x-moose': 'majestic',
  'content-type': 'text/html; charset=UTF-8',
  'cache-control': 'no-cache' }
In [ ]:
> response.request.
response.request.__defineGetter__        response.request.__defineSetter__
response.request.__lookupGetter__        response.request.__lookupSetter__
response.request.constructor             response.request.hasOwnProperty
response.request.isPrototypeOf           response.request.propertyIsEnumerable
response.request.toLocaleString          response.request.toString
response.request.valueOf                 

response.request.__isRequestRequest      response.request._auth
response.request._callback               response.request._callbackCalled
response.request._destdata               response.request._disableCookies
response.request._ended                  response.request._events
response.request._jar                    response.request._maxListeners
response.request._multipart              response.request._oauth
response.request._redirect               response.request._started
response.request.agent                   response.request.agentClass
response.request.callback                response.request.dests
response.request.domain                  response.request.getHeader
response.request.hasHeader               response.request.headers
response.request.host                    response.request.href
response.request.httpModule              response.request.localAddress
response.request.method                  response.request.ntick
response.request.originalCookieHeader    response.request.originalHost
response.request.originalHostHeaderName  response.request.path
response.request.pool                    response.request.port
response.request.proxy                   response.request.qsLib
response.request.qsParseOptions          response.request.qsStringifyOptions
response.request.readable                response.request.removeHeader
response.request.req                     response.request.response
response.request.setHeader               response.request.setHost
response.request.tunnel                  response.request.uri
response.request.writable                

> response.request.headers
{}
> response.request.headers.
response.request.headers.__defineGetter__      response.request.headers.__defineSetter__
response.request.headers.__lookupGetter__      response.request.headers.__lookupSetter__
response.request.headers.constructor           response.request.headers.hasOwnProperty
response.request.headers.isPrototypeOf         response.request.headers.propertyIsEnumerable
response.request.headers.toLocaleString        response.request.headers.toString
response.request.headers.valueOf               
In [ ]:
> response.request.agent   
{ domain: null,
  _events: { free: [Function] },
  _maxListeners: 10,
  options: {},
  requests: {},
  sockets: { 'www.reddit.com:80': [ [Object] ] },
  maxSockets: 5,
  createConnection: [Function] }
> 


Посты чуть ниже также могут вас заинтересовать

Комментариев нет:

Отправить комментарий