Javascript express'response.sendFile()发送未捕获的语法错误:意外标记

Javascript express'response.sendFile()发送未捕获的语法错误:意外标记,javascript,express,reactjs,react-router,Javascript,Express,Reactjs,React Router,我正在使用browserHistory。此功能: app.get('*', function (request, response){ response.sendFile(path.join(__dirname + "/public/index.html")) }) 应该发送Index.html,因为路由是通过react路由器在客户端进行的。 但不知何故,服务器误解了我的index.html,我得到了以下错误: 未捕获的语法错误:意外标记

我正在使用browserHistory。此功能:

app.get('*', function (request, response){
  response.sendFile(path.join(__dirname + "/public/index.html"))
})
应该发送Index.html,因为路由是通过react路由器在客户端进行的。 但不知何故,服务器误解了我的index.html,我得到了以下错误:

未捕获的语法错误:意外标记<

我不知道这里有什么问题。文件路径是否错误?我的树看起来像这样:

/app
/app/myJavascriptStuff.js
/public
/public/index.html
/moreStuffThatsNotRelevant
/server.js <- my express file
如果没有上述功能,我的页面通常会响应:

什么都得不到

在localhost:3000上未发生的每一页刷新,例如localhost:3000/无论什么

因为我知道我在描述事物方面很差劲,所以它是指向存储库的链接


非常感谢您的帮助

问题可能是因为对于每个请求,即使浏览器通过请求bundle.js或style.css,您也会发送index.html

定义一个公共文件夹,从中可以提供这些文件,比如说public,在github repo中有index.html。让webpack在该文件夹中生成bundle.js。还可以指向index.html以使用该文件夹中的bundle.js

接下来,在server.js中,您需要做一些更改。当浏览器要求时,让express使用公共路径为这些文件提供服务

例如:

const app             = express();
const publicPath     = express.static(path.join(__dirname, 'public'), { redirect : false });

const indexPath  = path.join(__dirname, 'public/index.html');
app.use(publicPath);
app.get('/', function (_, res) { res.sendFile(indexPath) });
app.get('*', function (_, res) { res.sendFile(indexPath) });

问题可能是因为对于每个请求,即使浏览器通过请求bundle.js或style.css,您也会发送index.html

定义一个公共文件夹,从中可以提供这些文件,比如说public,在github repo中有index.html。让webpack在该文件夹中生成bundle.js。还可以指向index.html以使用该文件夹中的bundle.js

接下来,在server.js中,您需要做一些更改。当浏览器要求时,让express使用公共路径为这些文件提供服务

例如:

const app             = express();
const publicPath     = express.static(path.join(__dirname, 'public'), { redirect : false });

const indexPath  = path.join(__dirname, 'public/index.html');
app.use(publicPath);
app.get('/', function (_, res) { res.sendFile(indexPath) });
app.get('*', function (_, res) { res.sendFile(indexPath) });

首先,您没有使用Web包构建bundle.js文件。在你能提供它之前,你需要先建造它

将package.json脚本更改为

"scripts": {
    "build": "webpack --progress --color --config  webpack.config.js --watch",
    "start": "node server.js"
  }
并在运行服务器之前运行命令npm run build

如果您没有在/public/build中创建webpack bundle.js,请在public中创建一个目录build,然后再次运行上述命令


这应该可以解决您的问题。

首先,您没有使用Web包构建bundle.js文件。在你能提供它之前,你需要先建造它

将package.json脚本更改为

"scripts": {
    "build": "webpack --progress --color --config  webpack.config.js --watch",
    "start": "node server.js"
  }
并在运行服务器之前运行命令npm run build

如果您没有在/public/build中创建webpack bundle.js,请在public中创建一个目录build,然后再次运行上述命令


这应该可以解决您的问题。

您的index.html是什么样子的?我在问题中提到了:你的index.html看起来像什么?我在问题中提到了:谢谢。这整个网页包的事情还是让我有点困惑:“没问题,很乐意帮忙:谢谢。”。整个网页包的事情仍然让我有点困惑:“没问题,很乐意帮助: