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

# Express

> 使用 Firecrawl 和 Express 构建网页抓取与搜索 API。

<div id="prerequisites">
  ## 前提条件
</div>

* Node.js 18+
* 一个 Firecrawl API 密钥 — [免费获取](https://www.firecrawl.dev/app/api-keys)

<div id="setup">
  ## 设置
</div>

```bash theme={null}
npm install express firecrawl
```

将你的 API 密钥添加到 `.env` 文件中：

```bash theme={null}
FIRECRAWL_API_KEY=fc-YOUR-API-KEY
```

<div id="search-the-web">
  ## 进行网页搜索
</div>

```javascript theme={null}
import express from "express";
import { Firecrawl } from "firecrawl";

const app = express();
app.use(express.json());

const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY });

app.post("/search", async (req, res) => {
  try {
    const { query } = req.body;
    const results = await firecrawl.search(query, { limit: 5 });
    res.json(results);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => console.log("Server running on port 3000"));
```

<div id="scrape-a-page">
  ## 抓取网页
</div>

```javascript theme={null}
app.post("/scrape", async (req, res) => {
  try {
    const { url } = req.body;
    const result = await firecrawl.scrape(url);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});
```

<div id="interact-with-a-page">
  ## 与页面交互
</div>

使用 interact 控制实时浏览器会话，可点击按钮、填写表单并提取动态内容。

```javascript theme={null}
app.post("/interact", async (req, res) => {
  try {
    const { url } = req.body;

    const result = await firecrawl.scrape(url, { formats: ['markdown'] });
    const scrapeId = result.metadata?.scrapeId;

    await firecrawl.interact(scrapeId, { prompt: 'Search for iPhone 16 Pro Max' });
    const response = await firecrawl.interact(scrapeId, { prompt: 'Click on the first result and tell me the price' });

    await firecrawl.stopInteraction(scrapeId);

    res.json({ output: response.output });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});
```

<div id="test-it">
  ## 试一试
</div>

```bash theme={null}
curl -X POST http://localhost:3000/search \
  -H "Content-Type: application/json" \
  -d '{"query": "firecrawl web scraping"}'
```

<div id="next-steps">
  ## 后续步骤
</div>

<CardGroup cols={2}>
  <Card title="抓取文档" icon="file-lines" href="/zh/features/scrape">
    包含所有抓取选项，如 formats、actions 和代理
  </Card>

  <Card title="Search 文档" icon="magnifying-glass" href="/zh/features/search">
    进行网页搜索并获取完整页面内容
  </Card>

  <Card title="交互文档" icon="hand-pointer" href="/zh/features/interact">
    点击、填写表单并提取动态内容
  </Card>

  <Card title="Node SDK 参考" icon="node" href="/zh/sdks/node">
    完整的 SDK 参考，涵盖爬取、map、batch 抓取 等
  </Card>
</CardGroup>
