執行環境套件管理器打包器測試執行器程式碼檢查格式化指南參考部落格意見回饋

使用 Express 和 Bun 建置 HTTP 伺服器

Express 和其他主要的 Node.js HTTP 庫應該可以在 Bun 中無需修改即可執行。Bun 實現了這些庫所相依的 node:http 和 node:https 模組。

terminal
bun add express

使用 Express 定義一個 HTTP 路由並啟動伺服器:

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 上啟動伺服器:

terminal
bun server.ts