Created
December 24, 2019 06:14
-
-
Save ryanlid/6150a06ce9d544281b7188673d16bebf to your computer and use it in GitHub Desktop.
HTTP2 client by Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http2 = require('http2'); | |
const fs = require('fs'); | |
const client = http2.connect('https://localhost', { | |
ca: fs.readFileSync('./ssl/localhost.pem') | |
}); | |
client.on('error', (err) => console.log(err)) | |
const req = client.request({ ':path': '/' }); | |
// 设置文件编码类型 | |
req.serEncoding('utf8') | |
let data = '' | |
// data 事件:发送请求数据 | |
req.on('data', (chunk) => { | |
data += chunk; | |
}); | |
// end事件:请求关闭,关闭客户端 | |
req.on('end', () => { | |
console.log('\n' + data); | |
client.close(); | |
}) | |
// 发送请求完毕,应该始终指定该方法 | |
req.end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment