Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created December 24, 2019 06:12
Show Gist options
  • Save ryanlid/9f63a22f30222e8b351d24e370d4649c to your computer and use it in GitHub Desktop.
Save ryanlid/9f63a22f30222e8b351d24e370d4649c to your computer and use it in GitHub Desktop.
HTTP2 Server by Node.js
const http2 = require('http2');
const fs = require('fs');
const server = http2.createSecureServer({
key: fs.readFileSync('./cert/domain.key'),
cert: fs.readFileSync('./cert/domain.pem')
});
server.on('error', (err) => console.log(err));
server.on('stream', (stream, header) => {
// 向客户端发送响应头信息
stream.respond({
'content-type': 'text/html',
':status': 200
});
// 向客户端发送文件内容,并结束请求
stream.end('<h1>Hello HTTP2</h1>')
})
server.listen(443);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment