Express.js,意外令牌< [英] Express.js, Unexpected token <
问题描述
我有一个简单的快递服务器,看起来像这样:
I have simple express server which looks like that:
Epxress应用程序:
var express = require('express');
var compression = require('compression');
var path = require('path');
var cors = require('cors');
var router = express.Router();
var app = express();
app.use('/bundle', express.static(path.join(__dirname, '/bundle')));
app.enable('trust proxy');
app.use(compression());
app.get('*', function(req, res) {
res.header('Cache-Control', "max-age=60, must-revalidate, private");
res.sendFile( path.join(__dirname, 'index.html') );
});
var server = app.listen(process.env.PORT || 5000, function () {
var host = server.address().address;
var port = server.address().port;
console.log(`Example app listening at http://%s:%s`, host, port);
});
简单的 html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Router test</title>
</head>
<body>
<div id="root"></div>
<script src="bundle.js"></script>
</body>
</html>
在bundle.js里面,我有带有客户端路由的ReactJS应用程序:
Inside of bundle.js i have ReactJS application with client-side routing:
render((
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="about" component={About} />
<Route path="about2" component={About2} />
<Route path="about3" component={About3} />
<Route path="*" component={NoMatch} />
</Route>
<Route path="*" component={NoMatch} />
</Router>
), document.getElementById('root'));
每当我尝试导航到 domain:port/(路由器支持此路由)时,一切正常. 但是,当我尝试导航到更复杂的URL(例如 domain:port///..等)时,浏览器出现错误:
Whenever i try to navigate to domain:port/ (this route is supported by router) everething is OK. But when i try to navigate to more complex URL, like domain:port///.. etc i got an error in browser:
bundle.js:1 Uncaught SyntaxError: Unexpected token <
看起来像不是从带有index.html的静态服务器响应中发送bundle.js,在bundle.js里面有html标记.
looke like instead of send bundle.js from static server response with index.html and inside bundle.js there is html markup.
我该如何解决?
谢谢!
推荐答案
似乎正在发生环回,因为*
规则每次都为index.html
提供服务,而当未找到bundle.js
时,它将将会投放index.html
,从而尝试将<
解析为javascript.
There seems to be a loopback occuring, since the *
rule is serving a index.html
every time, and when bundle.js
is not found, it will serve index.html
instead, thus trying to parse <
as javascript.
一种索引接收,如果您愿意...
A sort of index-ception if you will...
这篇关于Express.js,意外令牌<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!