Node.js https.server API解析
2014-05-07 16:51
495 查看
web加密传输技术:
/** * Created by Antianlu on 14-5-7. * HTTPS是一个基于TLS/SSL的HTTP协议,在Node中作为一个独立的模块实现 */ var https = require('https'); var fs = require('fs'); /** * 以下为全部参数列表,如果使用了出key和cert或者pfx之外的其他参数,是在客户端代理中使用。 * pfx: 证书, 用于SSL的私秘钥证书文件. 默认 null. * key: 为SSL提供秘钥. 默认 null. * passphrase: 为 私秘钥 或者 pfx提供通行证字符串. 默认 null. * cert: 使用公共 x509证书. 默认 null. * ca: 权威证书或数组的权威证书检查远程主机. * ciphers: A string describing the ciphers to use or exclude. Consult http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT for details on the format. * rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true. * secureProtocol: The SSL method to use, e.g. SSLv3_method to force SSL version 3. The possible values depend on your installation of OpenSSL and are defined in the constant SSL_METHODS. * @type {{key: *, cert: *}} */ var options = { key : fs.readFileSync('certs/agent2-key.pem'), cert: fs.readFileSync('certs/agent2-cert.pem') }; /** * 创建https服务器,很多于http服务器相同,只是部分参数配置不一 * @param {Object} options 配置 另一种配置为:var optinos = {pfx : fs.readFileSync('server.pfx')} * @param {Function} callback 回调函数 */ https.createServer(options,function(req,res){ res.writeHead(200); res.end('Hello world!\n'); }).listen(3000); // 创建一个安全的web请求服务 /** * 说明:一下为可选参数 * host:域名或IP地址的服务器发出请求.默认是 'localhost'. * hostname: 支持 url.parse() hostname是首选的主机 * port: 服务器远程端口.默认:443. * method: HTTP请求的方法字符串描述. 默认:'GET'. * path:请求路径,默认: '/'. 应该包括任何query字符串. E.G. '/index.html?page=12' * headers: 包含请求的头对象. * auth: 基本身份验证 i.e. 'user:password' to compute an Authorization header. * agent: 控制代理行为. 代理用于请求连接的默认connection是: keep-alive. 可能值有: undefined (default): 使用 globalAgent 的host和port. Agent object: 通过显示代理. false:代理连接池, 默认 Connection: close.[翻译有点问题] * @type {{hostname: string, port: number, path: string, method: string}} */ var options = { hostname:'encrypted.google.com', port:443, path:'/', method:'GET' }; var req = https.request(options,function(res){ console.log('statusCode: ',res.statusCode); console.log('headers: ',res.headers); res.on('data',function(d){ process.stdout.write(d); }); }); req.end(); req.on('error',function(e){ console.error(e); })
相关文章推荐
- 二、nodejs图片处理(gm) api: https://www.npmjs.com/package/gm 及 MD5加密:http://nodejs.cn/doc/node/crypto.html#crypto_crypto_createhash_algorithm
- nodejs Events -https://nodejs.org/api/events.html
- Node.js 解析gzip网页(https)
- Simple Nodejs Http/Https Proxy Server
- Node.Js API 解析--FS模块
- Nodejs Express 4.X 中文API 2--- Request篇
- 利用Node.js对某智能家居server重构
- Node.js中URL的解析
- Build Node.Js web server in Docker containers: nodejs+pm2+mongodb+redis
- Node.js基础学习之常见API理解
- Js nodeType 属性全面解析
- 解析Node.js异常处理中domain模块的使用方法
- 实现一个完整的Node.js RESTful API的示例
- 用Node.js创建自签名的HTTPS服务器
- Node.js安装和入门 - 2行代码让你能够启动一个Server
- Learning Server-Side JavaScript with Node.js
- 基于Zetta(Node.js)的数据接收端server中,可接收数量不定传感数据的IoT APP实现
- Node.js API 初解读(三)
- 拥抱_Node.js_8.0_N-API_入门极简例子
- 使用node.js进行API自动化回归测试