The JS runtimes
Published in

The JS runtimes

A React SSR hello world project in Deno

Introduction

Step 1

export { renderToReadableStream } from "https://esm.run/react-dom/server";
export * as React from "https://esm.run/react";
> deno cache deps.ts

Step 2

import { renderToReadableStream } from "./deps.ts";
import { React } from "./deps.ts";

const App = () => (
<html>
<body>
<h1>Hello World</h1>
<p>This is an example.</p>
</body>
</html>
);

const headers = {
headers: {
"Content-Type": "text/html",
"Cache-Control": "no-transform",
},
};

Deno.serve(
async (req) => {
return new Response(await renderToReadableStream(<App />), headers);
},
{ port: 3000 },
);

Step 3

{
"tasks": {
"start": "deno run --allow-net=:3000 --unstable server.tsx"
}
}

Step 4

> deno task start
Task start deno run --allow-net=:3000 --unstable server.tsx
Listening on http://127.0.0.1:3000/

--

--

Articles on the popular JS runtimes, Node.js, Deno, and Bun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store