濡備綍鍒涘缓 http2 node App

Posted JS鑿?/a> 濡備綍鍒涘缓

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了濡備綍鍒涘缓 http2 node App相关的知识,希望对你有一定的参考价值。

濡備綍鍒涘缓 http2 node App

猸愶笍 鏇村鍓嶇鎶€鏈拰鐭ヨ瘑鐐癸紝鎼滅储璁㈤槄鍙?JS 鑿?/code> 璁㈤槄

  • 鎵€鏈夋暟鎹互浜岃繘鍒朵紶杈?/code>锛堝垎鐗囨柟寮忎笉鍚岋紝HTTP2 涔嬪墠鏄瓧绗︿覆鐨勫舰寮忓彂閫侊級

  • 鍙戦€佺殑璇锋眰鍙互涓嶆寜鐓ч『搴?/code>鍙戦€?/p>

  • 澶翠俊鎭帇缂?/code>浠ュ強 Server Push锛堟湇鍔$涓诲姩鎺ㄩ€佸唴瀹癸級绛夐珮鏁堢巼鐨勫姛鑳?/p>

  • 淇¢亾澶嶇敤锛堝彧闇€瑕佸缓绔嬩竴涓?TCP 閾炬帴锛?/span>

  • 鍒嗗抚浼犺緭锛堝苟鍙戝彂閫佷笉鍚岃姹傦級

浣跨敤 HTTP2 鐨勫ソ澶勮繖閲屼笉鍐嶈禈杩颁簡锛岀綉涓婁竴澶у爢鏉愭枡鑷繁鏌?馃槅

杩欑瘒鐭枃绠€鍗曚粙缁嶄竴涓嬪浣曞湪 node 搴旂敤涓娇鐢?http2锛?/p>

  • 棣栧厛闇€瑕?SSL 璇佷功

  • 鍒涘缓鏈嶅姟绔?APP

  • 浠?express 涓轰緥瀹夎 spdy 妯″潡

鑷鍚?SSL 璇佷功

鐢熸垚绉侀挜 Key

openssl genrsa -des3 -out server.key 2048

涓婅堪鍛戒护浣跨敤 Triple-DES 绠楁硶鐢熸垚绉侀挜 server.key

# cat server.key 

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,EB5B873BE89A5456

GWpEoVlP7DA9S955gUVZgrWww+PTspxwFMEoUKN9Z0WgjR30Qa+OOC93eyeVw/Zz
UvE60mCocTP0iSOOUCobW9i/v012zH6//QngqfzahVqqCz5B3zOAaB+LKOj/1S9X
GL7P7OOGWXik4nCGoN1rGszK6TqtboAl4YM00si/bU0wgrCxdCLV4ISvtMFV1cLz
4I0E9fbJI3MBZWlgTlm1RlOo0vyUFjmPJm78qLJlyD7Mk4VZ7uBh973UArkYpZNt
8hCtwj+DoPRUo/lXsoH4J/W3ma7BlrEjg7PkKffnX/L6HTqPMWV2zK6mmeiBoSdH
zL+rc7V8mqntrsZ+6qkNbjOV27zBi47SdDPP8CRnsggO83U/yJsxgruDzs7/f1sM
...

瀹為檯涓婂氨鏄暱杩欐牱鐨勪竴涓叉枃鏈?/p>

鐢熸垚璇佷功绛惧悕璇锋眰鏂囦欢 CSR

鐢ㄦ潵缁欒瘉涔﹂鍙戞満鏋勪娇鐢ㄥ叾鏍硅瘉涔︾閽ョ鍚嶇敓鎴愯瘉涔﹀叕閽ョ殑涓滆タ

浣跨敤濡備笅鍛戒护鐢熸垚 CSR锛?/p>

openssl req -new -key server.key -out server.csr

闇€瑕佷娇鐢ㄤ笂涓€涓楠ょ敓鎴愮殑 server 绉侀挜鏉ュ垱寤轰竴涓?csr 璇佷功绛惧悕璇锋眰鏂囦欢

娉ㄦ剰杩欓噷杩橀渶瑕佽緭鍏ョ敓鎴愮閽ユ椂鍊欒缃殑瀵嗙爜 pass

Country Name (2 letter code) []:
State or Province Name (full name) []:
Locality Name (eg, city) []:
Organization Name (eg, company) []:
Organizational Unit Name (eg, section) []:
Common Name (eg, fully qualified host name) []:
Email Address []:
error, no objects specified in config file
problems making Certificate Request

浼氳闂竴绯诲垪闂 鍏ㄩ儴榛樿涓虹┖鏄笉琛岀殑 浣犲繀椤诲緱濉偣涓滆タ 馃樀

# cat server.csr

-----BEGIN CERTIFICATE REQUEST-----
MIIC6DCCAdACAQAwgYkxCzAJBgNVBAYTAkNOMRIwEAYDVQQIDAlndWFuZ2Rvbmcx
ETAPBgNVBAcMCHNoZW56aGVuMRAwDgYDVQQKDAd0ZW5jZW50MRAwDgYDVQQLDAdz
ZWN0aW9uMRAwDgYDVQQDDAdUZW5jZW50MR0wGwYJKoZIhvcNAQkBFg50ZXN0QGdt
YWlsLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALEcfCsfSxg9
Rc20riGI7j06u3kt5A9+s/RUWYjFMuh9oXpl7njUrZX6rdxA0Ckl+X/9JHjGmYpX
EO23hVCSfyK9fpMd9MiPs5CvFkll3GH7xomif1aRv/ZXkyvTSBpCjRdemysqRy8Y
i+3N8l0qnxIJ5A4LbV4QhjVL+4iv/0Y4zvvjuOY7Rvtm4vU1YiKCS2T6NdJ46Msu
ZRvm8VtKqWtjk1ZM+0iFE8rRFJZ1Jepj+5vtqcqz1s0gKpwZ+jFIhzieGXTuKsp0

闀胯繖鏍?/p>

鍒犻櫎鎺夌閽ヤ腑鐨勫瘑鐮佺煭璇?passphrase

涓哄暐瑕佸垹闄ょ湅杩欓噷锛歨ttps://blog.longwin.com.tw/2014/08/apache2-nginx-ssl-restart-auto-input-password-2014/

cp server.key server.pass.key
openssl rsa -in server.pass.key -out server.key

杩欐牱灏卞皢瀵嗙爜绉婚櫎浜?/p>

鐢熸垚鑷鍚嶈瘉涔?/span>

鏈€鍚庡氨瑕佺敓鎴愯嚜绛惧悕璇佷功浜嗭紝闇€瑕佷娇鐢ㄨ瘉涔︾鍚嶈姹傛枃浠?server.csr 鍜岀閽?server.key锛屾湁鏁堟湡涓€骞达細

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

鏈€鍚庡垹鎺?server.pass.key 鍗冲彲锛堟病鏈夊瘑閽ョ暀鐫€浼氭湁鍗遍櫓锛?/p>

鐢熸垚鐨勬枃浠跺湪鏌愪釜鏂囦欢澶瑰唴锛屽湪闇€瑕佺殑鏃跺€欒鍙栧嵆鍙細

.
鈹溾攢鈹€ server.crt
鈹斺攢鈹€ server.key

鍒涘缓鏈嶅姟绔?App

// app.js

const spdy = require("spdy");
const express = require("express");
const fs = require("fs");
const path = require("path");
const app = express();

app.get("/"function(req, res{
  res.send("hello world");
});

const options = {
  key: fs.readFileSync(path.resolve(__dirname, "../cert/server.key")),
  cert: fs.readFileSync(path.resolve(__dirname, "../cert/server.crt"))
};

spdy.createServer(options, app).listen(3000, err => {
  if (err) { throw new Error(err) }

  console.log("Listening at: " + 3000);
});

鐒跺悗 node app.js 杩愯锛屽皾璇曚娇鐢?curl 鍛戒护璁块棶锛?/p>

curl https://localhost:3000/ --insecure
curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
RangeError: Invalid typed array length: -4095

馃槓

灞呯劧鎶ラ敊锛岀綉涓婃悳浜嗕竴涓嬪彂鐜版槸 node 鐗堟湰闂锛?/p>

https://github.com/spdy-http2/node-spdy/issues/350

灏濊瘯鍒囨崲 node 鐗堟湰鍒?v10:

node -v
v10.15.3

鍐嶆鍚姩锛?/p>

[nodemon] 1.18.11
[nodemon] to restart at any timeenter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
Listening at: 3000.

鐢ㄦ祻瑙堝櫒璁块棶锛?/p>

濡備綍鍒涘缓 http2 node App

protocol: h2 ~ 馃殌

灏濊瘯鏈嶅姟绔?push

h2 鐨勪竴澶х壒鑹叉槸鏈嶅姟绔帹鐨勮兘鍔涳紝浣跨敤 spdy 杩欎釜妯″潡鑳借交鏉惧疄鐜拌繖涓姛鑳斤細

鍦ㄩ」鐩洰褰曢噷鏂板缓涓€涓浘鐗囷紝鎴戜滑鍑嗗浣跨敤 push 灏嗗浘鐗囩瓑闈欐€佽祫婧愭帹閫佸埌瀹㈡埛绔?/p>

淇敼 APP 浠g爜濡備笅锛?/p>

const spdy = require("spdy");
const express = require("express");
const fs = require("fs");
const path = require("path");
const app = express();

app.get("/app.js", (req, res) => {
  res.end(fs.readFileSync("./app.js"));
});

app.get("/", (req, res) => {
  res.push("/test.png", { method"GET" }).end(fs.readFileSync("./test.png"));
  //   res.push("/app.js", { method: "GET" }).end(fs.readFileSync('./app.js'));

  res.end(`
<html>
<head>
<script src="/app.js"></script>
</head>
<body><img src="/test.png"></body>
</html>
  `
);
});

const options = {
  key: fs.readFileSync(path.resolve(__dirname, "../cert/server.key")),
  cert: fs.readFileSync(path.resolve(__dirname, "../cert/server.crt"))
};

spdy.createServer(options, app).listen(3000, err => {
  if (err) {
    throw new Error(err);
  }

  console.log("Listening on port: " + 3000 + ".");
});

涓婇潰浠g爜寮€鍚簡 test.png 鍥剧墖鏂囦欢鐨?push锛岃€屾湭寮€鍚?js 鏂囦欢鐨?push 鏁堟灉濡備笅锛?/p>

濡備綍鍒涘缓 http2 node App

鍥剧墖纭疄浣跨敤浜?push

淇敼浠g爜锛?/p>

app.get("/", (req, res) => {
  res.push("/test.png", { method"GET" }).end(fs.readFileSync("./test.png"));
  res.push("/app.js", { method"GET" }).end(fs.readFileSync("./app.js"));

  // ...

鑴氭湰鏂囦欢涔熸垚鍔熶娇鐢ㄤ簡 push锛?/p>

done

鍙傝€冿細

  • https://www.akadia.com/services/ssh_test_certificate.html

璇峰叧娉ㄦ垜鐨勮闃呭彿锛屼笉瀹氭湡鎺ㄩ€佹湁鍏?JS 鐨勬妧鏈枃绔狅紝鍙皥鎶€鏈笉璋堝叓鍗?馃槉


以上是关于濡備綍鍒涘缓 http2 node App的主要内容,如果未能解决你的问题,请参考以下文章

浠庡ご鍒涘缓鎮ㄨ嚜宸辩殑vue.js鈥斺€旂2閮ㄥ垎(铏氭嫙DOM鍩虹)

濡備綍鍒╃敤Excel蹇€熸壒閲忕敓鎴愭兂瑕佺殑浠g爜

浜笢濡備綍鐜╄浆TensorFlow?

NodeJS gRPC濡備綍鍚敤鍘嬬缉

shell鍒涘缓鏁版嵁搴撶殑鑴氭湰

MFC 灞炴€ц〃鍗曠殑鍒涘缓

(c)2006-2024 SYSTEM All Rights Reserved IT常识