两天前我开始使用node.js。我用node.js制作了我的http服务器,并且我有一个几个月前出于某些测试目的制作的页面,但是当我在我的计算机上托管我的页面时,它可以工作,但没有包含在html页面中的javascript文件,例如jQuery和一些图形库。
我尝试使用 IE 调试器进行调试,但无法解决该问题。
有趣的是,当我从磁盘打开同一页面时,只需双击 .html
文件,它就可以正常运行,没有任何错误!
我的问题是我应该做什么才能工作?我是否错过了 Node.js 的某些内容?
我的页面上有一个 html5 canvas 元素,也许这是一个问题
这是我的index.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello Dialog</title>
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta http-equiv="Cache-Control" content="no-store">
<meta http-equiv="cache-control" content="max-age=0">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT">
<meta http-equiv="pragma" content="no-cache">
<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="RGraph.line.js"></script>
<script type="text/javascript" src="RGraph.common.core.js"></script>
<script type="text/javascript" src="RGraph.drawing.background.js"></script>
<script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="./js/jquery-ui-1.9.2.custom.min.js"> </script>
<script type="text/javascript" src="index.js"></script>
<script type="text/javascript" src="temperature(1).json"></script>
</head>
<body style="font-size: 10px;">
<button id="btn_hello">Say Hello</button>
<button id="btn_hello1">LUKA</button>
<button id="btn_hello2">ANJA</button>
<button id="btn_hello3">MARKO</button>
<div id="dlg_hello">Hello World!</div>
<canvas id="graph" width="600" height="500"></canvas>
<script>
var data = [];
var limit;
var Temp = [];
var TimeStamp = [];
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
readTextFile("temperature(1).json", function(text){
data = JSON.parse(text);
for(var i =0; i<Object.keys(data).length; i++)
{
//alert(parseFloat(data[i].TimeStamp));
TimeStamp[i] = data[i].TimeStamp;
Temp[i] = parseFloat(data[i].Value);
}
DrawGraph(Temp, TimeStamp);
});
function DrawGraph(data, timestamp)
{
var line = new RGraph.Line({
id: 'graph',
data: data,
options: {
labels: timestamp,
gutterLeft: 55,
gutterRight: 35,
gutterBottom: 35,
gutterTop: 35,
title: 'A basic line chart',
backgroundGridColor: '#aaa',
backgroundGridDashed: true,
textAccessible: true,
scaleZerostart: true,
labelsOffsety: 5
}
}).draw();
}
</script>
</body>
</html>
这是node.js 中的server.js
var http = require("http");
var fs = require("fs");
var windows1250 = require("windows-1250");
var ht = fs.readFileSync('./index.html',"utf-8");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':ht.length});
res.write(ht);
res.end();
console.log(req.url);
}).listen(8888, function () {console.log("server is listening on port 8888");});
最佳答案
服务器不知道在哪里可以找到您的 JavaScript 文件。您可能想考虑使用express.js。这提供了使用express.static命令的能力。
关于javascript - Node.js 服务器和 .html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40207574/