http serverを立てる
localのファイルを動かすだけなら、std/http/file_server.tsを直接動かすのが簡単
$ deno run --allow-net --allow-read https://deno.land/std@0.177.0/http/file_server.ts
localのHTMLファイルを動かす
code:sh
deno run --allow-read --allow-net=0.0.0.0:8899 https://scrapbox.io/api/code/deno-ja/http_serverを立てる/server.ts your-directory
code:server.ts
import { createApp, serveStatic } from "https://deno.land/x/servest@v1.3.1/mod.ts";
const directory = Deno.args0;
const app = createApp();
app.use(serveStatic(directory));
const port = 8899;
app.listen({ port });
console.log(Open http://localhost:${port});
JSX版
たったの1ファイルだけHTTP serverを立ててweb pageを動かせます
実行用
code:sh
deno run --allow-net https://scrapbox.io/api/code/deno-ja/http_server%E3%82%92%E7%AB%8B%E3%81%A6%E3%82%8B/jsx_server.tsx
Usage | keroxp/servestを改変しています
cf. https://twitter.com/keroxp/status/1181554197973495808
servestを使っています
servestとJSXを併用するときは、deno-typesをReactの型に指定しないとType Errorだらけになるので注意してください
code:jsx_server.tsx
// @deno-types="https://deno.land/x/servest@v1.3.1/types/react/index.d.ts"
import React from 'https://esm.sh/react@17.0.2';
// @deno-types="https://deno.land/x/servest@v1.3.1/types/react-dom/server/index.d.ts"
import ReactDOMServer from 'https://esm.sh/react-dom@17.0.2/server';
import { createApp } from "https://deno.land/x/servest@v1.3.1/mod.ts";
const app = createApp();
app.handle("/", async (req) => {
await req.respond({
status: 200,
headers: new Headers({
"content-type": "text/html; charset=UTF-8",
}),
body: ReactDOMServer.renderToString(
<html>
<head>
<meta charSet="utf-8" />
<title>servest</title>
</head>
<body>Hello Servest!</body>
</html>,
),
});
});
app.listen({ port: 8888 });
console.log('go to http://localhost:8888');