Created
February 10, 2019 02:08
-
-
Save Yoshinori-Koide-PRO/e45c93e532974d8defd25b2ac82c0cff to your computer and use it in GitHub Desktop.
denoでのHTTPサーバーのサンプル。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env deno --allow-net | |
import { | |
listenAndServe, | |
ServerRequest, | |
setContentLength, | |
Response | |
} from "https://deno.land/x/http/server.ts"; | |
import { cwd, DenoError, ErrorKind, args, stat, readDir, open } from "deno"; | |
const serverArgs = args.slice(); | |
const addr = `0.0.0.0:${serverArgs[1] || 4500}`; | |
listenAndServe(addr, async req =>{ | |
const body = new TextDecoder().decode(await req.body()); | |
const headers = new Headers(); | |
headers.set("content-type", "application/json"); | |
let res : Response = { | |
status : 200, | |
body: new TextEncoder().encode('{"postBody":"'+body+'"}'), | |
headers | |
}; | |
setContentLength(res); | |
req.respond(res); | |
}); | |
console.log(`HTTP server listening on http://${addr}/`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment