能否将Bunjs用作完整后端服务器?附Express代码运行疑问
Bun 能否作为完整后端服务器使用?
当然可以!Bun 不仅能用来启动 React 前端应用,完全胜任完整后端服务器的角色,而且对 Node.js 生态的兼容性极佳——你提供的 Express 代码直接就能在 Bun 环境下运行。
运行你给出的 Express 代码步骤:
- 确保已安装 Bun,初始化项目:
bun init -y - 安装 Express 依赖:
bun add express - 将代码保存为
index.js - 启动服务:
bun index.js
启动后访问 http://localhost:3000,就能看到"hello world"的输出。
另外,Bun 自身还提供了性能更出色的原生 HTTP 服务器 API,无需依赖 Express 也能搭建后端,示例代码如下:
const server = Bun.serve({ port: 3000, fetch(req) { return new Response("Hello from Bun's native server!"); }, }); console.log(`Server up at http://localhost:${server.port}`);
内容的提问来源于stack exchange,提问作者Mohamed Kamel
相关产品推荐
相关产品推荐

