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
- node之http.request
- node下使用request转发http请求
- Node.js之HTTP
- Jmeter之HTTP Request Defaults
- Jmeter之HTTP Request Defaults
- Jmeter之HTTP Request Defaults
- Jmeter之HTTP Request Defaults
- http协议之request详解
- JMeter之HTTP Request Defaults
- Jmeter之HTTP Request Defaults
- Node.js第三方库之request
- [NODE之17]request和express框架request
- Node request
- node-request()
- node通过http.request向其他服务器上传文件
- Adding timeout support for http request in Node.JS
- Using HTTP Proxy in Node.js with request package
- http请求利器---node.js中的request模块
- 欢迎使用CSDN-markdown编辑器
- 怎样设计一个好的数据库
- IT职场,技能比情商重要N倍
- 关于阅读陆舟老师《Struts2技术内幕-深入解析Struts2架构设计与实现原理》一书的阅读笔记(0)
- 什么是死锁,简述死锁发生的四个必要条件,如何避免死锁,解除死锁
- node之http.request
- centos 6.8安装oracle11g r2简易说明
- 算法精解----递归(自我总结)
- 数据库设计原则
- 今年美国广告程序化购买支出将超252亿美元
- Linux系统编程---实现目录或者文件拷贝
- C++静态库与动态库详解与使用
- maven,svn一键打包部署。
- 编译和链接