Skip to content
🌾A progressive http server / router for deno🌾
Branch: master
Clone or download
Latest commit 425f1da May 5, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
benchmark bump: deno -> v0.3.10, deno_std -> v0.3.2 May 3, 2019
fixtures bump: deno_std v0.4.0 May 4, 2019
.editorconfig fix: server request interface Mar 18, 2019
.gitignore add: first commit Mar 10, 2019
.licenserc.json fmt Mar 10, 2019
.travis.yml ci: bump deno v0.4.0 May 4, 2019
LICENSE Initial commit Mar 9, 2019
Makefile ci: bump deno v0.4.0 May 4, 2019
README.md Update README.md May 5, 2019
agent.ts bump: deno_std v0.4.0 May 4, 2019
agent_test.ts bump: deno_std v0.4.0 May 4, 2019
promises.ts add: agent api Mar 27, 2019
readers.ts bump: deno_std v0.4.0 May 4, 2019
readers_test.ts bump: deno_std v0.4.0 May 4, 2019
responder.ts fix: server request interface Mar 18, 2019
responder_test.ts bump: deno_std v0.4.0 May 4, 2019
router.ts bump: deno_std v0.4.0 May 4, 2019
router_test.ts bump: deno_std v0.4.0 May 4, 2019
serveio.ts bump: deno_std v0.4.0 May 4, 2019
serveio_test.ts
server.ts bump: deno_std v0.4.0 May 4, 2019
server_test.ts bump: deno_std v0.4.0 May 4, 2019
tests.ts bump: deno_std v0.4.0 May 4, 2019

README.md

servest

https://travis-ci.com/keroxp/servest https://img.shields.io/github/tag/keroxp/servest.svg license tag tag

🌾A progressive http server / router for deno🌾

Usage

Serve API

Serve API is compatible with deno_std@v0.4.0 but has different implementation. Some progressive features for HTTP/1.1 server are implemented.

  • Support Keep-Alive connection
  • Support trailer headers
  • Support keep-alive timeout and read timeout
  • serve and listenAndServe is cancellable by cancel promise
  • Fully interface based type definition
import { listenAndServe } from "https://denopkg.com/keroxp/servest@v0.7.1/server.ts";
listenAndServe(":8899", async req => {
  await req.respond({
      status: 200,
      headers: new Headers({
        "Content-Type": "text/plain"
      }),
      body: new TextEncoder().encode("hello")
    });
})

NOTE: use listenAndServe instead of serve

Generally listenAndServe has higher concurrency than serve because serve is built top of async iteration. yield in async iteration degrades concurrency of promises.

Processing of requests from identical keep-alive connection should be handled in serial, but requests from different connections should be handled in concurrent.

listenAndServe does it as it is built top of async callback. It is faster than serve about x2 in our benchmark test.

Router API

Router API is minimal routing system on top of listenAndServe()

import { createRouter } from "https://denopkg.com/keroxp/servest@v0.7.2/router.ts";

const router = createRouter();
router.handle("/", async req => {
  await req.respond({
    status: 200,
    headers: new Headers({
      "content-type": "text/plain"
    }),
    body: new TextEncoder().encode("ok")
  });
});
router.handle(new RegExp("/foo/(?<id>.+)"), async req => {
  const { id } = req.match.groups;
  await req.respond({
    status: 200,
    headers: new Headers({
      "content-type": "application/json"
    }),
    body: new TextEncoder().encode(JSON.stringify({ id }))
  });
});
router.listen("127.0.0.1:8898");

Agent API

Agent API is basic HTTP agent. It manages persistent connection to host. Each request will be sent in serial.

fetch is enough for most cases. It is useful if you want to manage keep-alive connection to host.

NOTE: Currently TLS (HTTPS) agent is not supported as Deno doesn't.

Use fetch for https request.

GET

import { createAgent } from "https://denopkg.com/keroxp/servest@v0.7.2/agent.ts";
const agent = createAgent("http://127.0.0.1:8700");
const { status, body } = await agent.send({
  path: "/get?deno=land",
  method: "GET"
});

POST

import { createAgent } from "https://denopkg.com/keroxp/servest@v0.7.2/agent.ts";
const { status, headers, body } = await agent.send({
  path: "/post",
  method: "POST",
  headers: new Headers({
    "Content-Type": "text/plain"
  }),
  body: new TextEncoder().encode("deno=land")
});

Loadmaps

  • Middleware API for Router
  • HTTP/2
  • HTTP testing api

License

MIT

Contributor

@keroxp

You can’t perform that action at this time.