mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-12 12:56:38 +00:00
enhance server
This commit is contained in:
parent
17377d6a98
commit
97506731e1
3 changed files with 34 additions and 7 deletions
|
|
@ -7,7 +7,7 @@
|
||||||
<option name="description" />
|
<option name="description" />
|
||||||
<option name="exitCodeBehavior" value="ERROR" />
|
<option name="exitCodeBehavior" value="ERROR" />
|
||||||
<option name="fileExtension" value="ts" />
|
<option name="fileExtension" value="ts" />
|
||||||
<option name="immediateSync" value="true" />
|
<option name="immediateSync" value="false" />
|
||||||
<option name="name" value="deno" />
|
<option name="name" value="deno" />
|
||||||
<option name="output" value="" />
|
<option name="output" value="" />
|
||||||
<option name="outputFilters">
|
<option name="outputFilters">
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
</option>
|
</option>
|
||||||
<option name="outputFromStdout" value="false" />
|
<option name="outputFromStdout" value="false" />
|
||||||
<option name="program" value="deno" />
|
<option name="program" value="deno" />
|
||||||
<option name="runOnExternalChanges" value="true" />
|
<option name="runOnExternalChanges" value="false" />
|
||||||
<option name="scopeName" value="Project Files" />
|
<option name="scopeName" value="Project Files" />
|
||||||
<option name="trackOnlyRoot" value="false" />
|
<option name="trackOnlyRoot" value="false" />
|
||||||
<option name="workingDir" value="" />
|
<option name="workingDir" value="" />
|
||||||
|
|
|
||||||
5
deps.ts
5
deps.ts
|
|
@ -4,4 +4,7 @@ export {
|
||||||
assertThrows,
|
assertThrows,
|
||||||
} from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
} from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
||||||
export { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
|
export { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
|
||||||
export { listenAndServe } from "https://deno.land/std@0.87.0/http/server.ts";
|
export {
|
||||||
|
listenAndServe,
|
||||||
|
ServerRequest,
|
||||||
|
} from "https://deno.land/std@0.87.0/http/server.ts";
|
||||||
|
|
|
||||||
32
server.ts
32
server.ts
|
|
@ -1,8 +1,32 @@
|
||||||
import { listenAndServe } from "./deps.ts";
|
import { listenAndServe, ServerRequest } from "./deps.ts";
|
||||||
|
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
|
||||||
|
|
||||||
const port = 8000;
|
const port = 8000;
|
||||||
|
|
||||||
console.log(`Server listening on http://localhost:${port}/`);
|
const parseURL = (url: string) => {
|
||||||
await listenAndServe({ port }, (req) => req.respond({ body: "Hello" }));
|
const parts = url.slice(1).split("/");
|
||||||
|
return parts[0];
|
||||||
|
};
|
||||||
|
|
||||||
// write handler using req.url
|
const serve = async (port: number) => {
|
||||||
|
console.log(`Server listening on http://localhost:${port}/`);
|
||||||
|
|
||||||
|
await listenAndServe({ port }, (req: ServerRequest) => {
|
||||||
|
console.log(`Request on address '${req.url}'`);
|
||||||
|
|
||||||
|
if (parseURL(req.url) === "") {
|
||||||
|
return req.respond({ body: sayRandomHello() });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const value = parseInt(parseURL(req.url), 10);
|
||||||
|
const id = new ID(value);
|
||||||
|
return req.respond({ body: sayHello(id) });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return req.respond({ body: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
await serve(port);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue