Skip to content

Commit

Permalink
fix: router interface
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Mar 18, 2019
1 parent dd6dc05 commit af858f8
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions router.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { createResponder, ServerResponder } from "./responder.ts";
import { IncomingHttpRequest, serve, ServeOptions } from "./server.ts";
import { encode } from "https://deno.land/[email protected]/strings/strings.ts";
import {IncomingHttpRequest, serve, ServeOptions} from "./server.ts";
import {encode} from "https://deno.land/[email protected]/strings/strings.ts";

export type RoutedServerRequest = IncomingHttpRequest & {
match?: RegExpMatchArray;
};

/** basic handler for http request */
export type HttpHandler = (
req: RoutedServerRequest,
res: ServerResponder
req: RoutedServerRequest
) => unknown;

/**
Expand Down Expand Up @@ -63,24 +61,23 @@ export function createRouter(): HttpRouter {
pathname,
routes.map(v => v.pattern)
);
const res = createResponder(req.bufWriter);
if (index > -1) {
const { handlers } = routes[index];
for (const handler of handlers) {
await handler(Object.assign(req, { match }), res);
if (res.isResponded()) {
await handler(Object.assign(req, { match }));
if (req.isResponded()) {
break;
}
}
if (!res.isResponded()) {
await res.respond({
if (!req.isResponded()) {
await req.respond({
status: 500,
headers: new Headers(),
body: encode("Not Responded")
});
}
} else {
await res.respond({
await req.respond({
status: 404,
headers: new Headers(),
body: encode("Not Found")
Expand Down

0 comments on commit af858f8

Please sign in to comment.