SyntaxError:expected expression,got'<' [英] SyntaxError: expected expression, got '<'
问题描述
我执行以下节点代码时,控制台中的 SyntaxError:expected expression,get'<'
错误
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
})
错误:
我使用的是Angular Js,它的文件夹结构就像bellow
I'm using Angular Js and it's folder structure like bellow
我在这里缺少什么?
推荐答案
此代码:
app.all('*', function (req, res) {
res.sendFile(__dirname+'/index.html') /* <= Where my ng-view is located */
})
告诉Express,无论浏览器要求什么,您的服务器应返回的index.html
。因此,当浏览器请求像 jquery-xyzmain.js
或 angular.min.js
之类的JavaScript文件时,您的服务器是返回 index.html
的内容,以<!DOCTYPE html>
开头,导致JavaScript错误。
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.js
or angular.min.js
, your server is returning the contents of index.html
, which start with <!DOCTYPE html>
, which causes the JavaScript error.
您的回调中的代码应该是查看请求,以确定要发送的文件,和/或您应该使用不同的路径模式与 app.all
。有关详细信息,请参阅路线指南。
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.
这篇关于SyntaxError:expected expression,got'<'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!