简体   繁体   中英

SyntaxError: expected expression, got '<'

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

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 :在此处输入图像描述

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

在此处输入图像描述

What's I'm missing here ?

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-xyzmain.js or angular.min.js , your server is returning the contents of index.html , which start with <!DOCTYPE html> , which causes the JavaScript error.

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 guide for details.

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.

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:

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

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

I think what @TJ 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.

Hope this works!

The main idea is that somehow HTML has been returned instead of 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

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

Make sure that assets had been successfully precompiled.

I had same error. And in my case

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

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

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:

<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 '<'

Just simply add:

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

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

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.

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

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.

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

This should work for you. If you are using 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'));
});

I was getting this error when trying to access an angular SPA via ASP.NET Core. Turns out the environment variables were not being loaded correctly, and the following line was not being called:

app.UseSpaStaticFiles();

After I ensured the line was getting called, the app started working again.

I solved this problem with my authenticated ASPNET application by adding WebResource.axd as a location on the web.config so that it's authorized for anonymous users

<location path="WebResource.axd">
  <system.web>
    <authorization>
      <allow users="?"/>
    </authorization>
  </system.web>
</location>

I got this type question on Django, and My issue is forget to add static to the <script> tag.

Such as in my template:

<script  type="text/javascript"  src="css/layer.js"></script>

I should add the static to it like this:

<script type="text/javascript" src="{% static 'css/layer.js' %}" ></script>

May be this helps someone. The error I was getting was

SyntaxError: expected expression, got '<'

What I did clicked on view source in chrome and there I found this error

: Undefined variable: string in on line :未定义变量:第字符串

The issue was this snippet of code,

$string= ''; <---- this should be declared outside the loop

while($row = mysql_fetch_assoc($result))
    {
        $string .= '{ id:'.$row['menu_id'].', pId:'.$row['parent_id'].', name:"'.$row['menu_name'].'"},';
    }

which fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM