node.js 创建 https 服务器

来源:互联网 发布:mysql 用户登录失败 编辑:程序博客网 时间:2024/05/16 07:17
openssl version -aOpenSSL 1.0.1 14 Mar 2012built on: Tue Jun  4 07:26:06 UTC 2013platform: debian-amd64options:  bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx)compiler: cc -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -DTERMIO -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wa,--noexecstack -Wall -DOPENSSL_NO_TLS1_2_CLIENT -DOPENSSL_MAX_TLS1_2_CIPHER_LENGTH=50 -DMD32_REG_T=int -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASMOPENSSLDIR: "/usr/lib/ssl"~ openssl genrsa -out privatekey.pem 1024~ openssl req -new -key privatekey.pem -out certrequest.csr ~ openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem



  1. var https = require('https'),  
  2.     url = require('url'),  
  3.     fs = require('fs');  
  4.   
  5. var options = {  
  6.     key:  fs.readFileSync('./privatekey.pem'),  //带路径的文件名,注意两个文件不要写反了  
  7.     cert:fs.readFileSync('./certificate.pem')  
  8. };  
  9.   
  10. https.createServer(options, function(req, res) {  
  11.     var data = '',  
  12.         reqUrl = decodeURIComponent(req.url);  
  13.         parse = url.parse(reqUrl, true),  
  14.         query = parse.query,  
  15.         path = parse.pathname;  
  16.     req.on('data'function(chunk) {  
  17.         data += chunk;   
  18.     });  
  19.     console.log(query);  
  20.     res.writeHead(200);  
  21.     res.end('hello world\n');  
  22. }).listen(8080);  

3)运行程序

4)在浏览器中访问:https://localhost:8080?id=1

//注,?号后面的参数随便带,看实际需要



使用post方式访问https服务器:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var https = require('https');  
  2. var querystring = require('querystring');  
  3. var post_data = querystring.stringify({  
  4.     act:'add',  
  5.     data:'1'  
  6. });  
  7. var options = {  
  8.     host:'localhost',  
  9.     port: 8889,  
  10.     path: '/cd02',  
  11.     method:'POST',  
  12.     rejectUnauthorized: false,  //很多时候不加会访问出错  
  13.     headers: {  
  14.         'Content-Type''application/x-www-form-urlencoded',  
  15.         'Content-Length': post_data.length  
  16.     }  
  17. };  
  18.   
  19. var req = https.request(options, function(res) {  
  20.     console.log('STATUS: ' + res.statusCode);  
  21.     console.log('HEADERS: ' + JSON.stringify(res.headers));  
  22.     res.on('error'function(error) {  
  23.         console.log('-----------');  
  24.         console.dir(error);  
  25.     });  
  26.     res.setEncoding('utf8');  
  27.     res.on('data'function (chunk) {  
  28.         console.dir(chunk);  
  29.     });  
  30. });  
  31.   
  32. req.on('error'function(err) {  
  33.     console.dir(err);  
  34. })  
  35.   
  36. // write data to request body  
  37. req.end(post_data);  
0 0
原创粉丝点击