Coder Social home page Coder Social logo

http2-http1-server-node's Introduction

http2-http1-server

CircleCI

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 of HTTP/2 Compatible API is not compatible like the following. The handler types are different.

// http
function createServer(options: ServerOptions, requestListener?: (req: IncomingMessage, res: ServerResponse) => void): Server;
// 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

npm install -S git+https://github.com/nwtgck/http2-http1-server-node

Usage

Here is an example to use.

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.

You can find whole example in examples/simple.

http2-http1-server-node's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar nwtgck avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.