Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created October 8, 2019 08:09
Show Gist options
  • Save hashrock/3929319391526221bb761f521e35cdaa to your computer and use it in GitHub Desktop.
Save hashrock/3929319391526221bb761f521e35cdaa to your computer and use it in GitHub Desktop.
One file server with deno
import { serve } from "https://deno.land/std@v0.19.0/http/server.ts";
import React from "https://dev.jspm.io/react";
import ReactDOMServer from "https://dev.jspm.io/react-dom/server";
const s = serve(":8000");
const hello: string = "Hello"
const View = (props) =>
<div className="deno">{hello}, {props.msg}</div>;
window.onload = async () => {
console.log("http://localhost:8000/");
for await (const req of s) {
const matches = req.url.match(/\/?name=(.+)/)
const name = matches && matches[1]
const str = ReactDOMServer.renderToString(<View msg={name ? name : "None" } />);
const body = new TextEncoder().encode(str);
req.respond({ body });
}
};
@Soremwar
Copy link

Impressive, but not working on Deno v0.20

@hashrock
Copy link
Author

@Soremwar
This is tested with v0.20.0 on Ubuntu(WSL).
The Windows version of deno doesn't seem to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment