首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js:未定义JustGage

Node.js:未定义JustGage
EN

Stack Overflow用户
提问于 2018-02-20 16:53:34
回答 1查看 616关注 0票数 0

我试图写一个简单的基于node.js的应用程序,可以使用JustGage (基于JavaScript的插件)。下面是我的Node.js应用程序。

代码语言:javascript
复制
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页面。

代码语言:javascript
复制
<!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包来解决这个问题

代码语言:javascript
复制
npm install justgage

然而,当我写下

代码语言:javascript
复制
var jg = require('justgage');

这显示了一个引用错误-文档未定义。请帮帮我!

EN

回答 1

Stack Overflow用户

发布于 2018-02-20 16:57:26

node.js没有为您的静态文件夹提供服务。

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

当您通过node.js运行时,您只为index.html提供服务,而不是相关资产。

this线程中讨论了一些提供静态文件/文件夹的有用方法

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48881150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档