Skip to content

Instantly share code, notes, and snippets.

@guilt
Last active December 28, 2022 22:26
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 guilt/271ea053f1a10fd7117f4bad3fc8fe49 to your computer and use it in GitHub Desktop.
Save guilt/271ea053f1a10fd7117f4bad3fc8fe49 to your computer and use it in GitHub Desktop.
Deno React App
import React from "https://esm.sh/react@17.0.2";
const App = () => {
return (
<h1>Hello Deno Land!</h1>
);
};
export default App;
import React from "https://esm.sh/react@17.0.2";
import ReactDOMServer from "https://esm.sh/react-dom@17.0.2/server";
import { Drash } from "https://deno.land/x/drash@v1.4.4/mod.ts";
import App from "./client.tsx";
const clientAppFile = "client.tsx";
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit("./client.tsx");
const browserBundlePath = "/browser.bundle.js";
const browserBundleContent = Object.entries(files).filter( ([fileName, text]) => fileName.endsWith("/client.tsx.js") )[0][1];
//Application Routes
class HomeResource extends Drash.Http.Resource {
static paths = ["/"];
static html =
`<!DOCTYPE html>
<html>
<head><script type="module" src="${browserBundlePath}"></script></head>
<body><div id="root">${ReactDOMServer.renderToString(<App />)}</div></body>
</html>`;
public GET() {
this.response.headers.set("Content-Type", "text/html");
this.response.body = HomeResource.html;
return this.response;
}
}
class BrowserBundleResource extends Drash.Http.Resource {
static paths = [browserBundlePath];
public GET() {
this.response.headers.set("Content-Type", "application/javascript");
this.response.body = browserBundleContent;
return this.response;
}
}
//Application
const appOptions = {resources: [HomeResource, BrowserBundleResource]};
const app = new Drash.Http.Server(appOptions);
//Bind Host and Port
const listenHostName = Deno.env.get("HOST") || "0.0.0.0";
const listenPort = +(Deno.env.get("PORT") || "4500");
const listenOptions = { hostname: listenHostName, port: listenPort};
//Run Application Loop
await app.run(listenOptions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment