2016-09-16 134 views
-1

當我運行的節點運行以下命令:請求瘋狂節點JS

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 } } 
+0

你在交互式終端運行? –

回答

-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); 
    } 
}); 
0

當使用交互模式的Node.js(發生這種情況,通過調用剛剛node),最後陳述的評價結果​​將自動打印到控制檯:

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

就你而言,它是表示請求的對象。如果您將該代碼移動到JavaScript源文件並運行它(「node file.js」),則該輸出將不再發生。出於實驗目的,假設您真的想在交互模式下使用Node.js,只需讓該語句返回其他內容即可。對此有不止一種方法。你可以在你的代碼的末尾添加一個較短的值:

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 

或 結果放在一個變量:

/* variable declarations return undefined */ 
var req = 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); 
    } 
});