// 导入必要的模块
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
 
// 设置静态文件目录
app.use(express.static('public'));
 
// 导入HTML文件
fs.readFile('routes/index.html', 'utf8', (err, html) => {
  if (err) {
    console.error('读取文件出错:', err);
    return;
  }
  app.get('/', (req, res) => {
    res.send(html);
  });
});
 
// 设置Nginx反向代理
app.all('/api/*', (req, res) => {
  // 假设Nginx运行在localhost的80端口
  req.pipe(request(req)).pipe(res);
});
 
// 设置CORS头部支持跨域请求
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});
 
// 监听3000端口
app.listen(3000, () => {
  console.log('服务器运行在 http://localhost:3000/');
});这段代码示例展示了如何在Node.js的Express框架中设置静态文件目录、导入HTML文件、配置Nginx反向代理以及处理CORS问题。在实际开发中,这些技术和实践都非常有用,并且是构建现代Web应用的标准做法。