Javascript 语法错误:预期的表达式,得到“<”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28894074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:33:52  来源:igfitidea点击:

SyntaxError: expected expression, got '<'

javascriptangularjsnode.jsexpress

提问by underscore

I got SyntaxError: expected expression, got '<'error in the console when i'm executing following node code

SyntaxError: expected expression, got '<'当我执行以下节点的代码在控制台错误

var express = require('express');
var app = express();
app.all('*', function (req, res) {
  res.sendFile(__dirname+'/index.html') /* <= Where my ng-view is located */
})
var server = app.listen(3000, function () {
  var host = server.address().address
  var port = server.address().port
})

Error :enter image description here

错误 :在此处输入图片说明

I'm using Angular Js and it's folder structure like bellow

我正在使用 Angular Js,它的文件夹结构如下所示

enter image description here

在此处输入图片说明

What's I'm missing here ?

我在这里缺少什么?

采纳答案by T.J. Crowder

This code:

这段代码:

app.all('*', function (req, res) {
  res.sendFile(__dirname+'/index.html') /* <= Where my ng-view is located */
})

tells Express that no matter what the browser requests, your server should return index.html. So when the browser requests JavaScript files like jquery-x.y.z.main.jsor angular.min.js, your server is returning the contents of index.html, which start with <!DOCTYPE html>, which causes the JavaScript error.

告诉 Express 无论浏览器请求什么,您的服务器都应该返回index.html. 因此,当浏览器请求jquery-x.y.z.main.js或 之类的 JavaScript 文件时angular.min.js,您的服务器将返回 以index.html开头的内容<!DOCTYPE html>,这会导致 JavaScript 错误。

Your code inside the callback should be looking at the request to determine which file to send back, and/or you should be using a different path pattern with app.all. See the routing guidefor details.

您在回调中的代码应该查看请求以确定要发回哪个文件,和/或您应该使用不同的路径模式与app.all. 有关详细信息,请参阅路由指南

回答by devonj

Try this, usually works for me:

试试这个,通常对我有用:

app.set('appPath', 'public');
app.use(express.static(__dirname +'/public'));

app.route('/*')
  .get(function(req, res) {
    res.sendfile(app.get('appPath') + '/index.html');
  });

Where the directory public contains my index.html and references all my angular files in the folder bower_components.

目录 public 包含我的 index.html 并引用文件夹 bower_components 中的所有角度文件。

The express.static portion I believe serves the static files correctly (.js and css files as referenced by your index.html). Judging by your code, you will probably need to use this line instead:

我认为 express.static 部分可以正确地提供静态文件(index.html 引用的 .js 和 css 文件)。根据您的代码判断,您可能需要改用这一行:

app.use(express.static(__dirname));

as it seems all your JS files, index.html and your JS server file are in the same directory.

因为看起来你所有的 JS 文件,index.html 和你的 JS 服务器文件都在同一个目录中。

I think what @T.J. Crowder was trying to say was use app.route to send different files other than index.html, as just using index.html will have your program just look for .html files and cause the JS error.

我认为@TJ Crowder 想说的是使用 app.route 发送 index.html 以外的不同文件,因为仅使用 index.html 将使您的程序只查找 .html 文件并导致 JS 错误。

Hope this works!

希望这有效!

回答by Nick Roz

The main idea is that somehow HTML has been returned instead of Javascript.

主要思想是不知何故返回了 HTML 而不是 Javascript。

The reason may be:

原因可能是:

  • wrong paths
  • assets itself
  • 错误的路径
  • 资产本身

It may be caused by wrong assets precompilation. In my case, I caught this error because of wrong encoding.

可能是资源预编译错误导致的。就我而言,由于编码错误,我发现了这个错误。

When I opened my application.js I saw application error Encoding::UndefinedConversionError at /assets/application.js

当我打开 application.js 时,我看到 application error Encoding::UndefinedConversionError at /assets/application.js

There was full backtrace of error formatted as HTML instead of Javasript.

有完整的错误回溯,格式为 HTML 而不是 Javasript。

Make sure that assets had been successfully precompiled.

确保资产已成功预编译。

回答by Erlan

I had same error. And in my case

我有同样的错误。就我而言

REASON: There was restriction to that resources on serverand server was sending login page instead of javascript pages.

原因服务器上的资源受到限制,服务器正在发送登录页面而不是 javascript 页面。

SOLUTION: give access to user for resourses or remove restriction at all.

解决方案:允许用户访问资源或完全取消限制。

回答by gigawatt

You can also get this error message when you place an inline tag in your html but make the (common for me) typo that you forget to add the slash to the closing-script tag like this:

当您在 html 中放置内联标记时,您也可能会收到此错误消息,但会出现(对我来说很常见)错误,即您忘记将斜杠添加到结束脚本标记中,如下所示:

<script>
  alert("I ran!");
<script> <!-- OOPS opened script tag again instead of closing it -->

The JS interpreter tries to "execute" the tag, which looks like an expression beginning with a less-than sign, hence the error: SyntaxError: expected expression, got '<'

JS 解释器尝试“执行”标签,它看起来像一个以小于号开头的表达式,因此出现错误: SyntaxError: expected expression, got '<'

回答by Moolshankar Tyagi

Just simply add:

只需简单地添加:

app.use(express.static(__dirname +'/app'));

where '/app'is the directory where your index.html resides or your Webapp.

这里'/app'是目录,您的index.html居住或你的web应用。

回答by phil

I had a similar problem using Angular js. i had a rewrite all to index.html in my .htaccess. The solution was to add the correct path slashes in . Each situation is unique, but hope this helps someone.

我在使用 Angular js 时遇到了类似的问题。我在我的 .htaccess 中将所有内容重写为 index.html。解决方案是在 . 每种情况都是独一无二的,但希望这对某人有所帮助。

回答by Muhammad Khalid

This problem occurs when your are including file with wrong path and server gives some 404 error in loading file.

当您包含路径错误的文件并且服务器在加载文件时出现一些 404 错误时,会发生此问题。

回答by Birdie Golden

I had this same error when I migrated a Wordpress site to another server. The URL in the header for my js scripts was still pointing to the old server and domain name.

当我将 Wordpress 站点迁移到另一台服务器时,我遇到了同样的错误。我的 js 脚本标题中的 URL 仍然指向旧的服务器和域名。

Once I updated the domain name, the error went away.

一旦我更新了域名,错误就消失了。

回答by tigercosmos

This should work for you. If you are using SPA.

这应该对你有用。如果您使用的是 SPA。

app.use('/', express.static(path.join(__dirname, 'your folder')));
// Send all other requests to the SPA
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'your folder/index.html'));
});