javascript Uncaught SyntaxError: Unexpected token < in nodejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34105183/
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
Uncaught SyntaxError: Unexpected token < in nodejs
提问by ShankarGuru
I am getting the below screen-shot error when i try to serve my client-side code. When i am trying to run node server/server.js
当我尝试提供客户端代码时,出现以下屏幕截图错误。当我尝试运行节点 server/server.js 时
The below is my server.js code...
下面是我的 server.js 代码...
app.use(express.static(path.join(__dirname, "public")));
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'}));
app.all('/*', function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Access-Token,X-Key");
if(req.method === 'OPTIONS'){
res.status(200).end();
} else {
next();
}
});
app.all("/api/v1/*", [require('./middlewares/validateRequest')]);
app.use("/", require("./routes"));
app.use(function(req, res, next){
var err = new Error("Not found");
err.status = 404;
next(err);
});
Inside my routes/index.js
, i have the following for get request
.
在我的里面routes/index.js
,我有以下内容get request
。
router.get('*', function(req, res) {
res.sendfile('./public/index.html');
});
回答by Jonathan Morales Vélez
What usually happens is that when the browser request a javascript file, the server sends an html file. This is due to rules like app.get('*'...
. So we need to tell the server to send the static files first and then declare the rules, like shown below:
通常发生的情况是,当浏览器请求一个 javascript 文件时,服务器发送一个 html 文件。这是由于诸如app.get('*'...
. 所以我们需要告诉服务器先发送静态文件,然后声明规则,如下所示:
// Declare static folder to be served. It contains the js, images, css, etc.
app.use(express.static('build'));
// Serve the index.html for all the other requests so that the
// router in the javascript app can render the necessary components
app.get('*',function(req,res){
res.sendFile(path.join(__dirname+'/build/index.html'));
//__dirname : It will resolve to your project folder.
});
回答by ruichao
I think this may be caused by your /public/index.html file. When you include the js files in the index.html, there's some problem with the "src" property. Thus it will return a "not found" html file instead of the actual js file.
我认为这可能是由您的 /public/index.html 文件引起的。当您在 index.html 中包含 js 文件时,“src”属性存在一些问题。因此它将返回一个“未找到”的 html 文件而不是实际的 js 文件。
回答by praveenpds
my folder structure
我的文件夹结构
node(folder)
server.js
-client(f)
-index.html
-views(f)
-js(f)
-ctrl(f)
-service(f)
-app.js
-state.js
and
和
app.use(express.static('client'));
app.get('/index.html', function (req, res) {
res.sendfile(_dirname+'/index.html');
});
call this from Browser http://127.0.0.1:1956/index.html
从浏览器调用它http://127.0.0.1:1956/index.html
var server = app.listen(1956, function (req, res) {
var host = server.address().address
var port = server.address().port
console.log("app listening at", host, port)
});
it's working fine for me
它对我来说很好用
回答by Mostaffa Ossman
I found the solution, the node js application tries to compile the js files inside the public folder, that make a mistakes; you defined the public folder like this:
我找到了解决方案,node js应用程序尝试编译public文件夹中的js文件,出错;您像这样定义了公共文件夹:
app.use(express.static(path.join(__dirname, 'public')));
Try to define it using a virtual directory by adding a specific virtual folder name like this:
尝试通过添加特定的虚拟文件夹名称来使用虚拟目录定义它,如下所示:
app.use('/static',express.static(path.join(__dirname, 'public')));
This should be work;
这应该是工作;
回答by Oss
I guess part of the problem is in your router
我想问题的一部分在于你 router
In your routes/index.js
change the get
request to
在您routes/index.js
将get
请求更改为
router.get('/', function(req, res) {
res.sendfile('./public/index.html');
});
Also are you pipe-lining your assets using gulp
or something similar? If not then how are you serving your assets?
您是否也在使用gulp
或类似的东西来流水线化您的资产?如果不是,那么您如何为您的资产提供服务?
I recommend you either serve your assets staticallyor use gulp
我建议您要么静态地提供您的资产,要么使用gulp