https://github.com/nwtgck/http2-http1-server-node
HTTP/2-interface HTTP1 server for compatibility for Node.js/TypeScript
https://github.com/nwtgck/http2-http1-server-node
http-server http1 http1-1 http2 node nodejs
Last synced: 3 months ago
JSON representation
HTTP/2-interface HTTP1 server for compatibility for Node.js/TypeScript
- Host: GitHub
- URL: https://github.com/nwtgck/http2-http1-server-node
- Owner: nwtgck
- License: mit
- Created: 2019-03-24T05:18:12.000Z (about 7 years ago)
- Default Branch: develop
- Last Pushed: 2021-07-25T16:46:28.000Z (over 4 years ago)
- Last Synced: 2024-10-11T15:10:49.174Z (over 1 year ago)
- Topics: http-server, http1, http1-1, http2, node, nodejs
- Language: TypeScript
- Homepage:
- Size: 281 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# http2-http1-server
[](https://circleci.com/gh/nwtgck/http2-http1-server-node)
HTTP/2-interface HTTP1 server for compatibility for Node.js/TypeScript
## Purpose
The purpose of this project is for compatibility to support HTTP/1 clients over non-TLS in Node.js/TypeScript.
Unfortunately [current type definitions](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/a642ee239c2992dd1ca45a1a0baeaa4169f374cd) of [HTTP/2 Compatible API](https://nodejs.org/api/http2.html#http2_compatibility_api) is not compatible like the following. The handler types are different.
```ts
// http
function createServer(options: ServerOptions, requestListener?: (req: IncomingMessage, res: ServerResponse) => void): Server;
```
```ts
// http2
export function createServer(options: ServerOptions, onRequestHandler?: (request: Http2ServerRequest, response: Http2ServerResponse) => void): Http2Server;
```
So, Node.js developers in TypeScript should **implement two different types of handlers** to support both HTTP/1 and HTTP/2 over non-TLS.
To solve the annoying issue, this project provides HTTP/1 server whose interface is the same as `http2.createServer()`. This project allows you to implement only one handler(= `(request: Http2ServerRequest, response: Http2ServerResponse) => void`).
## Installation
```bash
npm install -S git+https://github.com/nwtgck/http2-http1-server-node
```
## Usage
Here is an example to use.
```ts
import { createServer } from "http2-http1-server";
import * as http2 from "http2";
import { readFileSync } from "fs";
const cert = readFileSync('./ssl_certs/server.crt');
const key = readFileSync('./ssl_certs/server.key');
const handler = (req: http2.Http2ServerRequest, res: http2.Http2ServerResponse) => {
res.end("hello from server!");
};
// [IMPORTANT] You can use the same handler!
const server = createServer({}, handler);
const secureServer = http2.createSecureServer({cert, key}, handler);
server.listen(8080);
secureServer.listen(8443);
```
* `server` supports only HTTP/1.
* `secureServer` supports both HTTP/1 and HTTP/2 because it uses [ALPN](https://nodejs.org/api/http2.html#http2_alpn_negotiation).
You can find whole example in [examples/simple](examples/simple).