Use middleware for Router

HttpRouter.use()adds global http handlers (middleware) to the router.

Middleware is called on every request and behaves pre-processor for requests. Here is an example of simple authentication middleware for web API.

// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { createRouter } from "https://servestjs.org/@v0.30.0/router.ts";
const router = createRouter();
// Called for every request
router.use(async req => {
  // Do authentication before handling request on routes
  const token = req.query.get("auth_token");
  if (token !== "valid_token") {
    // Responded request won't be passed to the next middleware
    await req.respond({ status: 401, body: "Unauthorized" });
  }
  // Go through the next middleware
});
router.listen(":8899");