> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Express 和 Bun 构建 HTTP 服务器

Express 和其他主要的 Node.js HTTP 库应该可以在 Bun 中无需修改即可工作。Bun 实现了这些库所依赖的 [`node:http`](https://nodejs.org/api/http.html) 和 [`node:https`](https://nodejs.org/api/https.html) 模块。

<Note>请参见 [Node.js 兼容性](/runtime/nodejs-compat#node-http) 了解详情。</Note>

```sh terminal icon="terminal" theme={null}
bun add express
```

***

要使用 Express 定义 HTTP 路由并启动服务器：

```ts server.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
import express from "express";

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

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

app.listen(port, () => {
  console.log(`正在监听端口 ${port}...`);
});
```

***

要在 `localhost` 上启动服务器：

```sh terminal icon="terminal" theme={null}
bun server.ts
```
