我试图写一个简单的基于node.js的应用程序,可以使用JustGage (基于JavaScript的插件)。下面是我的Node.js应用程序。
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(8080);以下是index.html页面。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Temperature Sensor</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="static/resources/js/justgage.js"></script>
<script type="text/javascript" src="static/resources/js/raphael-2.1.4.min.js"></script>
<script>
window.onload = function(){
var g = new JustGage({
id: "g_a411_faculty",
value: 22,
min: 10,
max: 40,
title: "A411 - Faculty Office",
label: "Celsius",
levelColors: [
"#ffffff",
"#f9c802",
"#FF0000"
]
});
};
</script>
</head>
<body>
<div id="g_a411_faculty" class="col-sm-6" style="width:400px; height:320px"></div>
</body>
</html>当我运行代码并在Google Chrome中打开http://localhost:8080时,索引页面显示一个错误,即JustGage未定义(在开发人员工具中)。然而,当我在Chrome中简单地打开index.html (不使用Node.js)时,页面工作正常。你能帮帮我吗?我哪里出错了?
我试图通过安装justgage包来解决这个问题
npm install justgage然而,当我写下
var jg = require('justgage');这显示了一个引用错误-文档未定义。请帮帮我!
发布于 2018-02-20 16:57:26
node.js没有为您的静态文件夹提供服务。
当直接从该文件夹运行时,该网站可以正常工作,因为您可以从同一位置访问static/...的相对路径。
当您通过node.js运行时,您只为index.html提供服务,而不是相关资产。
在this线程中讨论了一些提供静态文件/文件夹的有用方法
https://stackoverflow.com/questions/48881150
复制相似问题