res.sendFile 问题
发布于 4 个月前 作者 tkdzhangyue 474 次浏览 来自 问答

求高手指点,node index.js 可以正常启动,显示正在监听端口,但是不能访问localhost:3000 。 一个简单的小项目,利用smoothie.js对数据实时可视化。

直接贴代码: index.js

var express=require('express');
var app=express();
var http=require('http').Server(app);
var io=require('socket.io')(http);
var dgram = require('dgram');
var server = dgram.createSocket('udp4');

app.get('/',function(req,res){
	res.sendFile(__dirname+'/index.html');
});
  
server.on('message', function(msg, rinfo) {
    console.log('服务端获取信息:'+msg+'来自:'+rinfo.address+':'+rinfo.port);
	io.sockets.emit('data',msg);
});
  
server.on('listening', function() {
    var address = server.address();
    console.log('服务端正在监听:'+address.address+':'+address.port);
});
  
server.bind(7);

index.html

<!doctype html>
<html>
	<head>

  </head>
  <body onload="createTimeline()">

    <p>The <em>hello world</em> of <a href="../">Smoothie Charts</a>. View source.</p>

    <canvas id="chart" width="400" height="100"></canvas>

      <script type="text/javascript" src="smoothie.js"></script>
    
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
	<script type="text/javascript">
	var data1 = 0;
	var socket = io('http://localhost:3000');
	
	socket.on('data',function(data){
		data1 = data;
	});
      // Randomly add a data point every 500ms
      var random = new TimeSeries();

      setInterval(function() {
        random.append(new Date().getTime(), data1);
      }, 500);
      
      function createTimeline() {
        var chart = new SmoothieChart();
        chart.addTimeSeries(random, { strokeStyle: 'rgba(0, 255, 0, 1)', fillStyle: 'rgba(0, 255, 0, 0.2)', lineWidth: 4 });
        chart.streamTo(document.getElementById("chart"), 500);
      }
	  
	  
    </script>

</body>
</html>
1 回复

看不懂你这个程序逻辑

https://github.com/Ireoo/chat/blob/master/server.js 这是我以前写的,供你参考下,虽然功能不一样,但意思差不多

回到顶部