Bunの使い方です。
Ubuntu 24.04 にインストール
sudo snap install bun-js
バージョンの確認
$ bun --version
1.1.12
TypeScript のプログラムを走らせる
プログラム
index.tsx
const server = Bun.serve({
port: 3000,
fetch(request) {
return new Response("Bun へようこそ!");
},
});
console.log(`Listening on localhost:${server.port}`);
Bun で実行
bun run index.tsx
クライアントで接続
http http://localhost:3000
実行結果
$ http http://localhost:3000
HTTP/1.1 200 OK
Content-Length: 20
Date: Mon, 10 Jun 2024 08:39:03 GMT
content-type: text/plain;charset=utf-8
Bun へようこそ!
JavaScript のプログラムを走らせる
プログラム
http.js
export default {
port: 3000,
fetch(request) {
return new Response("皆さん こんにちは!")
},
}
Bun で実行
bun run http.js
クライアントで接続
http http://localhost:3000
実行結果
$ http http://localhost:3000
HTTP/1.1 200 OK
Content-Length: 28
Date: Mon, 10 Jun 2024 08:41:40 GMT
content-type: text/plain;charset=utf-8
皆さん こんにちは!