server.js 497 B

1234567891011121314151617
  1. const express = require('express');
  2. const path = require('path');
  3. const app = express();
  4. const port = process.env.PORT || 3004;
  5. // 提供 React 编译后的静态文件
  6. app.use(express.static(path.join(__dirname, 'build')));
  7. // 处理 SPA 路由(前端路由),如果没有匹配的文件,返回 index.html
  8. app.get('*', (req, res) => {
  9. res.sendFile(path.join(__dirname, 'build', 'index.html'));
  10. });
  11. app.listen(port, () => {
  12. console.log(`Server is running on port ${port}`);
  13. });