Skip to content

Instantly share code, notes, and snippets.

@Yoshinori-Koide-PRO
Created February 10, 2019 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yoshinori-Koide-PRO/e45c93e532974d8defd25b2ac82c0cff to your computer and use it in GitHub Desktop.
Save Yoshinori-Koide-PRO/e45c93e532974d8defd25b2ac82c0cff to your computer and use it in GitHub Desktop.
denoでのHTTPサーバーのサンプル。
#!/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