Bun

指南生態系統

使用 Express 和 Bun 建立 HTTP 伺服器

Express 和其他主要的 Node.js HTTP 函式庫應該可以直接使用。Bun 實作了這些函式庫依賴的 node:httpnode:https 模組。

請參閱 執行時期 > Node.js API 頁面,以取得更詳細的相容性資訊。

bun add express

若要定義一個簡單的 HTTP 路由並使用 Express 啟動伺服器

server.ts
import express from "express";

const app = express();
const port = 8080;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(port, () => {
  console.log(`Listening on port ${port}...`);
});

若要在 localhost 上啟動伺服器

bun server.ts