简体   繁体   English

未定义node.js index.html javascript函数

[英]node.js index.html javascript function not defined

I have an ' index.html ' page that references a javascript file of mine, ' bargraph1.js ', that is in the same directory as index.html . 我有一个“ index.html ”页面,该页面引用了我的JavaScript文件“ bargraph1.js ”,该文件与index.html处于同一目录中。 In the html file, I call a function in that js page: 在html文件中,我在该js页面中调用了一个函数:

<!DOCTYPE html>
<html lang="en">
   <head>
     <meta charset="utf-8">
     <title>D3 Test</title>

     <script type="text/javascript" src="d3.v4/d3.js"></script>
     <script type="text/javascript" src="bargraph1.js"></script>

  </head>
  <body>
    <h2>BarGraph</h2>
      <script type="text/javascript">
         barGraph(null, null, null, 30);
      </script>

  </body>
</html> 

The file bargraph1.js contains one function ' barGraph() ' that uses d3 to render a bar graph. 文件bargraph1.js包含一个函数' barGraph() ',该函数使用d3渲染条形图。

If I open this file with 'file open' in my browser, it renders beautifully with no errors in the browser error console. 如果我在浏览器中使用“文件打开”功能打开该文件,则它在浏览器错误控制台中的呈现效果很漂亮,没有任何错误。

HOWEVER, if I try to serve the page from a nodejs server, the bar graph does not appear and the console shows ' barGraph not defined '. 但是,如果我尝试从Node.js服务器提供页面,则条形图不会出现,控制台会显示“ barGraph not defined ”。 The h2 header preceeding it does show up, though. 不过,确实会出现在它前面的h2标头。 (Actually I don't think it can find anything in any of the referenced .js files when served by the server). (实际上,当服务器提供服务时,我认为它无法在任何引用的.js文件中找到任何内容)。 Here's the server code: 这是服务器代码:

var app  = require ('express')();
var http = require ('http').Server(app);
var io   = require ('socket.io')(http);

var pagePath = __dirname + '/index.html';
console.log('sending page ' + pagePath + '\'');
app.get ('/', function(req, res) { res.sendFile(pagePath); });

io.on ('connection', function(socket) {
   console.log ('\nsocket.id ' + socket.id + ' connected');

   socket.on ('disconnect', function() {
      console.log ('\nsocket.id ' + socket.id + ' disconnected');
   });
});

//===================================================
// set HTTP server to listen on port 3001
//===================================================

http.listen (3001, function() {
    console.log(' (3) http listening on *:3001 '); 
});

SO - obviously there is something different about how things get resolved (paths?) in these two different ways to show the html page with javascript. 所以-显然,通过两种不同的方式来显示带有javascript的html页面,如何解决问题(路径?)存在一些不同。 I have been using this same bargraph1.js file many times in files opened locally in the browser so I am sure there are no syntax errors in it. 我已经在浏览器本地打开的文件中多次使用了相同的bargraph1.js文件,因此我确信其中没有语法错误。

How can I get this file and the functions contained in it to work with a nodejs server? 如何获得此文件及其包含的功能以与Node.js服务器一起使用?

Thanks 谢谢

You need to create routes for the URLs inside the <script> tags. 您需要为<script>标记内的URL创建路由。 Otherwise, when your browser requests those script files, it will get a 404 from the Node server (this doesn't happen when you just double-click the HTML file because your computer automatically serves those files using directories as URL paths). 否则,当浏览器请求这些脚本文件时,它将从节点服务器获取404(仅双击HTML文件不会发生这种情况,因为您的计算机会使用目录作为URL路径自动提供这些文件)。 Since you're using Express now, try running 由于您现在正在使用Express,因此请尝试运行

app.use(express.static(path.resolve(__dirname, '/')));

and then at the end instead of doing http.listen , run 然后最后,而不是执行http.listen ,运行

app.listen(3001, process.env.IP || "0.0.0.0", function() {
    console.log(' (3) http listening on *:3001 '); 
});

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

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