Express 和其他主要的 Node.js HTTP 庫可以在 Bun 上開箱即用。Bun 實現了這些庫所依賴的 node:http 和 node:https 模塊。
NOTE
請參閱 [運行時 > Node.js API](/zh-cn/runtime/nodejs-compat#node-http) 頁面獲取更詳細的兼容性 信息。sh
bun add express要使用 Express 定義一個簡單的 HTTP 路由並啟動服務器:
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 上啟動服務器:
sh
bun server.ts