角度-意外令牌< [英] Angular - Unexpected token <
问题描述
我分别使用angular-cli
和express
命令行设置了angular和express应用程序.我已经使用ng build
构建了角度应用程序,当我想将它与Express Server一起使用时,在Google Chrome控制台中出现以下错误:
I have set up an angular and express app with angular-cli
and express
command lines respectively. I've already build angular app with ng build
and when I want to serve it with express server I get following error in google chrome console:
Uncaught SyntaxError: Unexpected token < runtime.js:1
Uncaught SyntaxError: Unexpected token < polyfills.js:1
Uncaught SyntaxError: Unexpected token < styles.js:1
Uncaught SyntaxError: Unexpected token < vendor.js:1
Uncaught SyntaxError: Unexpected token < main.js:1
这是我的server.js文件:
here is my server.js file:
.
.
.
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/users', usersRouter);
app.use('/auth', authRouter);
// Catch all other routes and return the angular index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/dist/client/index.html'));
});
index.html文件:
index.html file:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Client</title>
<base href="./">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="runtime.js"></script>
<script type="text/javascript" src="polyfills.js"></script>
<script type="text/javascript" src="styles.js"></script>
<script type="text/javascript" src="vendor.js"></script>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
ng serve
没问题,应用程序将平稳运行,而且当我仅使用浏览器打开index.html时,它也能正常工作,我认为这与快递服务器有关.
There is no problem with ng serve
and app will run smoothly and also when I just open index.html with browser it also works, I think it somehow relates to express server.
推荐答案
我认为问题是您的脚本文件是从根级别加载的,从而使浏览器请求http://example.com/XXX.js
.这就是为什么它们落在Express的*
路线中的原因.这意味着每个对XXX.js的请求都会获得相同的index.html
文件,而JS引擎无法正确解析该文件.
I think the issue is that your script files are loaded from the root level making your browser request http://example.com/XXX.js
. That's why they fall in the Express' *
route. This means every request for XXX.js gets the same index.html
file which can't be properly parsed by JS engine.
这篇关于角度-意外令牌<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!