NodeJs:意外的令牌<JavaScript文件中的错误

我正在创build一个单一和最简单的网页forms,我已经为Nodejs创build了一个js文件:看起来像这样:

var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { fs.readFile('treeDemoinJavaScript.html', function (err, html) { if (err) { throw err; } res.writeHeader(200, {'Content-Type': 'text/html'}); res.write(html); res.end(); }); }).listen(8090); 

然后是文件treeDemoinJavaScript.html,看起来像这样:

 <!DOCTYPE html> <html> <body> <br> <h2>Linked List in Javascript</h2> <p>Click the button to sort the array in ascending order.</p> INPUT <input type="text" id="inputList"> </input> <button onclick="insertInTree()">Add in Tree</button> </br> <button onclick="insertIntoTree()" id="show">Show in Tree</button></br></br> <button onclick="search()" id="search">Search in Tree</button> <input type="text" id="searchtextbox" visibility="hidden" placeholder="type the input "> </input> <button onclick="deletefunction()" id="delete">Delete from Tree</button> <p id="demo">welcome, the array is as follows: </p> <p id="demo2">welcome, the array is as follows: </p> <script type="text/javascript" src="treeDemoInJavaScript.js"> </script> </body> </html> 

现在什么问题是,即使我放置一个简单的警报函数在我的treeDemoInJavaScript.js文件,不工作,当我通过做$terminal节点startNodeDemo.js运行然后在铬我得到以下错误,像这样: 在这里输入图像说明

错误显示: – 意外的令牌< ,我不明白为什么浏览器运行treeDemoInJavaScript.js文件,并显示错误。

你的代码的问题是,对于每个请求,它读取treeDemoinJavaScript.html ,添加一个text/html的头部值,并将HTML文件的内容写入响应。

如果你的浏览器需要treeDemoInJavaScript.js ,它会从服务器请求它,但是服务器会发送一个HTML文件。 浏览器不理解它,并因此引发错误。

首先,我会推荐阅读这篇文章 。 它将指导您如何使用NodeJS设置服务器。 您使用的代码定义了非常低级别的服务器交互。 强烈build议使用ExpressJS等专用框架。

来解决你的问题, 这个简单的要点将帮助你使你的代码服务JS文件。

第一:

在你的服务器端:

 res.writeHeader(200, {'Content-Type': 'text/html'}); 

这是一个错字错误,而不是res.writeHeader ,你应该使用res.writeHead ,如下所示:

 res.writeHead(200, { 'Content-Type': 'text/html' }); 

第二:

您正在响应所有请求作为文件treeDemoinJavaScript.html的内容。 当页面加载时,这一行将向服务器发出另一个请求:

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

由于你的错误,它默认使用treeDemoInJavaScript.html的内容来响应文件treeDemoInJavaScript.js ,从<!DOCTYPE html> ,它不是JavaScript的有效语法,所以错误来了。 要解决,请参阅@ 31piy的答案。

那么我在浪费了一天之后就解决了我的问题。 我通过两种方式做到了这一点:

  1. 按照@ 31piy的build议使用Express.js,从express.js开始,我通过npm install express –save命令创build并安装了Express.js,并且得到了一个package.json文件。 现在,故事从创build目录结构开始。 但是在这个博客中,他们所犯的错误是,他们没有引导你介绍express.js中的静态文件,这是必不可less的,因为我在任何地方都添加了我的JavaScript文件,但是它没有读取它。

所以我需要做的是创build一个公共文件夹并行视图文件夹,然后将我的JavaScript文件中。 然后使用app.use(express.static('public'))来让Express知道需要包含一些静态文件。 然后直接在它里面使用javascript.js

 <!DOCTYPE html> <br> <h2>Linked List in Javascript</h2> <p>Click the button to sort the array in ascending order.</p> INPUT <input type="text" id="inputList"> </input> <button onclick="insertInTree()">Add in Tree</button> </br> <button onclick="insertIntoTree()" id="show">Show in Tree</button></br></br> <button onclick="search()" id="search">Search in Tree</button> <input type="text" id="searchtextbox" visibility="hidden" placeholder="type the input "> </input> <button onclick="deletefunction()" id="delete">Delete from Tree</button> <p id="demo">welcome, the array is as follows: </p> <p id="demo2">welcome, the array is as follows: </p> <script type="text/javascript" src="javascript.js"> </script>