Failed to load resource: the server responded with a status of 404 (Not Found)
发布于 4 个月前 作者 tkdzhangyue 723 次浏览 来自 问答

123.js `` $(function () {

$(document).ready(function () {

		var sensordata = 0;
        var socket = io.connect();  
        socket.on('data', function (obj) {//接收到服务器发送过来的名为'new'的数据  
            sensordata = boj.dataname;
			//console.log(obj.dataname);//data为应服务器发送过来的数据。  
            //socket.emit('my new event', { my:'new data' });//向服务器发送数据,实现双向数据传输  
        });
		
    Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });
    $('#container').highcharts({
        chart: {
            type: 'spline',
            animation: Highcharts.svg, // don't animate in old IE
            marginRight: 10,
            events: {
                load: function () {
                    // set up the updating of the chart each second
                    var series = this.series[0];
                    setInterval(function () {
                        var x = (new Date()).getTime(), // current time
                            y = sensordata;
							//y = Math.random();
                        series.addPoint([x, y], true, true);
                    }, 1000);
                }
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + '</b><br/>' +
                    Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                    Highcharts.numberFormat(this.y, 2);
            }
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Random data',
            data: (function () {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;
                for (i = -19; i <= 0; i += 1) {
                    data.push({
                        x: time + i * 1000,
                        y: Math.random()
                    });
                }
                return data;
            }())
        }]
    });
});

});

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’);

http.listen(3000, function () { console.log(‘Server listening at port %d’, 3000); });

app.get(’/’,function(req,res){ res.sendFile(__dirname+’/123.html’); });

server.on(‘message’, function(msg, rinfo) { console.log(‘服务端获取信息:’+msg+‘来自:’+rinfo.address+’:’+rinfo.port); io.sockets.emit(‘data’,{ dataname:msg }); });

server.on(‘listening’, function() { var address = server.address(); console.log(‘服务端正在监听:’+address.address+’:’+address.port); });

server.bind(7);

123.html

<!DOCTYPE html> <html> <head> <meta charset="utf-8">
</head>
<body>
<div id="container" style="min-width:400px;height:400px"></div>
	<script src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>
	<script src="http://cdn.hcharts.cn/highcharts/highcharts.js"></script>
	<script src="123.js"></script>
	<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
</body>
</html> ``
4 回复

运行起来,chrome报错,找不到http://localhost:3000/123.js

app.get(’/123.js’,function(req,res){ res.sendFile(__dirname+’/123.js’); 增加这行

@haply 感激不尽,今天我按照框架做简单多了

问题搞定了,回顾我遇到的坑! 实现功能:在express不用框架+socket.io+dgram 完成单片机udp传输数据到html5的hcharts。 用express框架socket.io握手不成功 不用express前端css js资源加载不上。 最后这句代码搞定。css js image等文件夹放在public下。 app.use(express.static(path.join(__dirname, ‘public’))); 但是奇葩的是,之前从网页复制粘贴和手打到app.js中完全不起作用。在express 项目名 -e 生成的项目文件夹下找app.js复制黏回来放在app.js顶部,好用了。 完!

回到顶部