繁体   English   中英

Node.js:未定义JustGage

[英]Node.js: JustGage is not defined

我正在尝试编写一个可以使用JustGage(基于JavaScript的插件)的基于node.js的简单应用程序。 以下是我的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时,索引页显示错误(未在Developer tools中定义JustGage)。 但是,当我仅在Chrome中打开index.html(不使用Node.js)时,该页面就可以正常工作。 您能帮我哪里出错吗?

我试图通过安装justgage软件包来解决此问题

npm install justgage

但是,当我写

var jg = require('justgage');

这显示了参考错误-未定义文档。 请帮忙!

node.js不提供您的静态文件夹。

当直接从文件夹运行时,该网站可以运行,因为您可以从同一位置访问static/...的相对路径。

通过node.js运行时,您仅提供index.html,而不提供相关资产。

线程中讨论了一些服务静态文件/文件夹的有用方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM