Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ app(
),
get("/error", () => [500, "an error has occured"]),
get("/callName", ({ params }) => `Hi, ${params.name}!`),
post("/callName", ({ params }) => `Hi, ${params.name}!`),
post("/callName", ({ body }) => `Hi, ${body.name}!`),
get("/foo", () => redirect("/hello", 302)), // redirect from /foo to /hello
get("/info", () => [
200,
Expand Down
1 change: 1 addition & 0 deletions handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type Context = {
readonly path: string;
readonly method: Method;
params: Params;
body: any;
};

export type Handler = BasicHandler | AsyncHandler;
Expand Down
14 changes: 8 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export class App {
} catch (e) {
// Do nothing here.
}
if (fileInfo && fileInfo.isDirectory()) {
if (fileInfo && fileInfo.isDirectory) {
staticFilePath += "/index.html";
try {
fileInfo = await stat(staticFilePath);
} catch (e) {
fileInfo = null; // FileInfo is not needed any more.
}
}
if (!fileInfo || !fileInfo.isFile()) {
if (!fileInfo || !fileInfo.isFile) {
return null;
}
return [
Expand All @@ -84,11 +84,13 @@ export class App {
req: ServerRequest,
): Promise<Response | null> {
const map = this.handlerMap.get(method);

if (!map) {
return null;
}

const params: Params = {};
let body: Object | null = null;

let handler;

Expand Down Expand Up @@ -152,21 +154,20 @@ export class App {
Object.assign(params, parseURLSearchParams(decodedBody));
break;
case "application/json":
let obj: Object;
try {
obj = JSON.parse(decodedBody);
body = JSON.parse(decodedBody);
} catch (e) {
throw ErrorCode.BadRequest;
}
Object.assign(params, obj);
//Object.assign(params, body);
break;
case "application/octet-stream":
// FIXME: we skip here for now, it should be implemented when Issue #41 resolved.
break;
}
}

const ctx = { path, method, params };
const ctx = { path, method, params, body };
const res = handler(ctx);
if (res instanceof Promise) {
return await (res as Promise<Response>);
Expand Down Expand Up @@ -202,6 +203,7 @@ export class App {
throw ErrorCode.NotFound;
}
const [path, search] = req.url.split(/\?(.+)/);

try {
r = (await this.respond(path, search, method, req)) ||
(this.staticEnabled && (await this.respondStatic(path))) ||
Expand Down
4 changes: 2 additions & 2 deletions serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ const testCases: Array<testCase> = [
},
{
name: "valid post",
registered: post("/params", ({ params }) => [200, params.name]),
registered: post("/params", ({ body }) => [200, body.name]),
path: "params",
params: JSON.stringify({ name: "ben" }),
method: Method.POST,
expected: "ben",
},
{
name: "valid post with detailed content-type",
registered: post("/params", ({ params }) => [200, params!.name]),
registered: post("/params", ({ body }) => [200, body!.name]),
path: "params",
params: JSON.stringify({ name: "tom" }),
headers: { "content-type": "application/json; charset=utf-8" },
Expand Down
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/encoding/utf8.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/encoding/utf8.ts';
export * from "https://deno.land/std@v0.40.0/encoding/utf8.ts";
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/flags/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/flags/mod.ts';
export * from "https://deno.land/std@v0.40.0/flags/mod.ts";
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/http/server.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/http/server.ts';
export * from "https://deno.land/std@v0.40.0/http/server.ts";
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/testing/asserts.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from 'https://deno.land/std@v0.40.0/testing/asserts.ts';
export * from "https://deno.land/std@v0.40.0/testing/asserts.ts";