GithubHelp home page GithubHelp logo

hayai2's Introduction

Hayai2 HTTP/2 Server

Experimental opinionated HTTP/2 framework for Node.js. Get a HTTP/2 server up and running with a simple and elegant API.

API SUBJECT TO CHANGE

Partial Roadmap

  • Make headers use Headers object to remove complexity when interacting with http headers.
  • Add MediaType helper to ease interaction with Content-Type header.
  • Add a helper to ease interaction with Accept- content negotation headers.

Goals

  • Handle http/1.1, https/1.1 and http/2 out of the box.
  • Minimal configuration.
  • No surprises.
  • Strong helpers available by default.

Usage

Install:

yarn install @daecatt/hayai2

Use:

const { createServer } = require("hayai2");

const [cert, key] = await Promise.all([
	fs.readFile(PATH_TO_CRT),
	fs.readFile(PATH_TO_KEY)
]);

/**
 * No need to .listen, the server is automatically attached to given port.
 * A http-to-https server is automatically created on port 80 if the given port
 * is 443. To disable this `redirectHttp: false` can be used.
 */
createServer({ port: 443, cert, key }, async request => {
	/**
	 * Response stream is created from .respond call.
	 */
	const response = request.respond(200, {
		"content-type": "text/plain; charset=utf-8"
	});

	if (request.method === "HEAD") return response.end();
	return response.end(`Hello world!`);
});

Cookie "middleware" Example

API Subject to change.

const { createServer } = require("hayai2");
const Cookies = require("hayai2/cookies");

createServer(opts, async request => {
	// Get cookies and modify request.respond to handle outgoing cookies.
	const cookies = Cookies.bind(request);

	// Check whether a cookie exists
	cookies.has("session");

	// Set a cookie
	cookies.set("cookie-banner", "ok");

	// Get a cookie (this always returns "ok", as the cookie was set above
	// even though we haven't actually seen the user send the cookie back).
	cookies.get("cookie-banner");
});

hayai2's People

Contributors

daecatt avatar

Watchers

 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.