http服务器

  1. const http = require("http");
  2. const url = require("url");
  3. const querystring = require("querystring");
  4. let server = http.createServer((req, res) => {
  5. let pathname = url.parse(req.url).pathname;
  6. console.log("pathname: " + pathname);
  7. let body = "";
  8. req.on("data", (data) => {
  9. body += data;
  10. });
  11. req.on("end", () => {
  12. res.writeHead(200, { "content-type": "text/plain" });
  13. if (req.method === "GET") {
  14. let query = querystring.parse(url.parse(req.url).query);
  15. console.log("query:%j", query);
  16. res.end("http-server-get");
  17. } else if (req.method === "POST") {
  18. let query = querystring.parse(body);
  19. console.log("query:%j", query);
  20. res.end("http-server-post");
  21. }
  22. });
  23. });
  24. server.listen(1234, () => {
  25. console.log("服务器启动监听localhost:1336");
  26. });

http客户端

  1. const http = require("http");
  2. const querystring = require("querystring");
  3. const options = {
  4. hostname: "localhost",
  5. port: 1234,
  6. path: "/info/child?abc=123&name=china",
  7. method: "GET",
  8. // method: "POST",
  9. };
  10. const contents = querystring.stringify({
  11. name: "china",
  12. abc: 456,
  13. });
  14. const req = http.request(options, (res) => {
  15. console.log("status:" + res.statusCode);
  16. console.log("headers:" + res.headers);
  17. res.setEncoding("utf8");
  18. res.on("data", (data) => {
  19. console.log("response:" + data);
  20. });
  21. });
  22. // req.write(contents);
  23. req.end();

https服务器

https需要访问秘钥和证书文件

安装了openssl,生成证书

  1. /**
  2. * ssl证书生成
  3. **/
  4. openssl genrsa -out privatekey.pem 1024 【生成privatekey.pem私钥】
  5. openssl req -new -key privatekey.pem -out certrequest.csr 【生成certrequest.csr签名】
  6. openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem 【生成certificate.pem签名证书】

https服务默认端口443。

  1. const https = require("https");
  2. const url = require("url");
  3. const fs = require("fs");
  4. var options = {
  5. key: fs.readFileSync("../privatekey.pem"),
  6. cert: fs.readFileSync("../certificate.pem"),
  7. passphrase: "123456", //你的密码
  8. };
  9. const server = https.createServer(options, function (req, res) {
  10. var pathname = url.parse(req.url).pathname;
  11. console.log("pathname:" + pathname);
  12. res.writeHead(200, { "Content-Type": "text/plain" });
  13. res.end("hello-server");
  14. });
  15. server.listen(443, () => {
  16. console.log("服务器监听localhost:443");
  17. });

https客户端

  1. var options={
  2. hostname:"localhost",
  3. port:443,
  4. path:"/info?abc=123&name=China",
  5. method:"GET"
  6. };
  7. options.agent = new https.Agent(options);
  8. //构造https请求(客户端)【自签名证书非法,应该使用正式正式】
  9. var req=https.request(options,function(res)
  10. {
  11. console.log("STATUS:"+res.statusCode);
  12. console.log("HEADERS:%j",res.headers);
  13. res.setEncoding("utf8");
  14. res.on("data",function(chunk)
  15. {
  16. console.log("response:"+chunk);
  17. });
  18. });
  19. req.end();//发送请求

通过浏览器去验证一下你的https服务器,https://localhost:443/find?name=123

浏览器默认会阻止打开未认证的https页面,可以输入thisisunsafe