const express = require('express');
const cors = require('cors');
const axios = require('axios');

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

const PORT = 3000;

// 处理百度搜索请求
app.get('/search', async (req, res) => {
  const query = req.query.q; // 获取前端传来的搜索关键词
  try {
    const response = await axios.get(`https://www.baidu.com/s?wd=${encodeURIComponent(query)}`);
    res.send(response.data); // 将百度返回的 HTML 数据转发给前端
  } catch (error) {
    res.status(500).send('Error fetching data from Baidu');
  }
});

app.listen(PORT, () => {
  console.log(`Proxy server running on http://localhost:${PORT}`);
});
