node之http.request

来源:互联网 发布:java有趣好玩的代码 编辑:程序博客网 时间:2024/04/29 18:19

问题提出:自己本机监听一个端口,创建一个server。局域网下,其他机器向自己机器发送请求。

解决方案:在http.request(options,cb)的options中进行处理。

查看http.request的源码,如下

http.request(options[, callback])#Added in: v0.3.6Node.js maintains several connections per server to make HTTP requests. This function allows one to transparently issue requests.options can be an object or a string. If options is a string, it is automatically parsed with url.parse().Options:protocol: Protocol to use. Defaults to 'http:'.host: A domain name or IP address of the server to issue the request to. Defaults to 'localhost'.hostname: Alias for host. To support url.parse() hostname is preferred over host.family: IP address family to use when resolving host and hostname. Valid values are 4 or 6. When unspecified, both IP v4 and v6 will be used.port: Port of remote server. Defaults to 80.localAddress: Local interface to bind for network connections.socketPath: Unix Domain Socket (use one of host:port or socketPath).method: A string specifying the HTTP request method. Defaults to 'GET'.path: Request path. Defaults to '/'. Should include query string if any. E.G. '/index.html?page=12'. An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future.headers: An object containing request headers.auth: Basic authentication i.e. 'user:password' to compute an Authorization header.agent: Controls Agent behavior. When an Agent is used request will default to Connection: keep-alive. Possible values:oundefined (default): use http.globalAgent for this host and port.oAgent object: explicitly use the passed in Agent.ofalse: opts out of connection pooling with an Agent, defaults request to Connection: close.createConnection: A function that produces a socket/stream to use for the request when the agent option is not used. This can be used to avoid creating a custom Agent class just to override the default createConnection function. Seeagent.createConnection() for more details.The optional callback parameter will be added as a one time listener for the 'response' event.http.request() returns an instance of the http.ClientRequest class. The ClientRequest instance is a writable stream. If one needs to upload a file with a POST request, then write to the ClientRequest object.

发现有个参数名称为host,这个是填写主机地址的,默认为localhost。

修改为你要访问的主机的即可,如下实例:

"use strict"  var http =require('http');  var config = {      "port":"3000",      "method":"POST",      "path":"null",      "host":"192.168.2.36"  }    /** * 描述:向服务端发送数据 * @param path "/update" 请求路径 * @param content {operation:"SVNUpdate"} 发送到服务端的数据 */  function $post(path,content){      config.path = path;      var request = http.request(config,function(res){          res.setEncoding('utf8');          var data = "";          res.on("data",function(chunk){              data += chunk;          })          res.on('end',function(){              console.log(data.toString())          })      })            request.write(JSON.stringify(content));      request.end();  }    $post("/book",{operation:"SVNUpdate"})  


0 0
原创粉丝点击