我正在使用Express
和create-react-app
。
我的 React 应用程序是一种方式,现在我正在尝试从 Express 服务器提供它。
// server/app.js
const express = require('express');
const path = require('path');
const app = express();
// Serve static assets
app.use(express.static(path.resolve(__dirname, '..', 'build')));
// serve main file
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
module.exports = app;
(当我执行npm run build
.
我在 Chrome 中查看页面,加载页面时发生的事情localhost:3000
是 Console prints Uncaught SyntaxError: Unexpected Token <
,并且在 Sources 选项卡中,它显示我的 CSS 和 JS 文件的内容与index.html
:相同,如图所示。
这似乎是一个公认的问题,所以希望有人以前见过这个。我什至不知道从哪里开始,特别是因为我实际上是从 Express 成功地为应用程序提供服务的。然后这开始发生,然后它在 git 分支的一些随机切换并恢复和重播更改后停止,然后它再次开始发生。所以我什至不确定是什么让它发生或不发生。