React路由器参数化的路由:SyntaxError:预期表达式,得到了“<" [英] React router parametrized routes: SyntaxError: expected expression, got '<'

查看:129
本文介绍了React路由器参数化的路由:SyntaxError:预期表达式,得到了“<"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用参数化路由的React-router设置:

I have a React-router set up which uses parametrized routes:

<Route path="/comments" comments={comments.commentsArray} component={NewReactElement} />
    <Route path="/comments/:id" component={Comment} />

错误:SyntaxError: expected expression, got '<'

我研究了该错误,并发现该错误是在服务器尝试获取.js/.css/other文件时发生的,但返回的是以<!DOCTYPE>开头的HTML,因此我设置了express.static ,但在输入comments/1250之类的URL时,它仍然返回:SyntaxError: expected expression, got '<'.这是我的服务器设置:

I have researched the error, and found out it was happening when the server tries to get a .js/.css/other file, but is returned HTML beginning with <!DOCTYPE> instead, so I set up the express.static, but when entering URL such as comments/1250, it still returns: SyntaxError: expected expression, got '<'. This is my server setup:

app.use(express.static(__dirname + '/views/webpacked'));
app.listen(5000);

app.get('*', (req, res) => {

    res.sendFile(path.resolve(__dirname, 'views', 'webpacked', 'index.html'));

 });

我也尝试过:

app.use('/*/*', express.static(...));

但是它也不起作用.

谢谢您的建议.

推荐答案

我有一个没有任何express的react/react-router页面,并且出现了相同的错误: SyntaxError: expected expression, got '<'开始出现,我配置了一个反应路径,而不仅仅是root /.

I've got a react/react-router page without any express and got the same error: SyntaxError: expected expression, got '<' which started to appear as soon as I configured a react route other then just root /.

经过一些试验,我发现在index.html中有一个指向js文件的链接:

After some experimenting I've figured out that in my index.html there was a link to js file:

  <script src="bundle.js"></script>
</body>
</html>

因此,解决方案是在源路径中添加/:

So, the solution was to add / in the source path:

  <script src="/bundle.js"></script>
</body>
</html>

,错误消失了.

希望能有所帮助.

这篇关于React路由器参数化的路由:SyntaxError:预期表达式,得到了“&lt;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆