使用 Express 和 Bun 构建 HTTP 服务器
Express 和其他主要的 Node.js HTTP 库应该可以在 Bun 中无需修改即可运行。Bun 实现了这些库所依赖的 node:http 和 node:https 模块。
terminal
bun add express
使用 Express 定义一个 HTTP 路由并启动服务器:
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