如何为“GET /socket.io/socket.io.js”处理400 Not Found?

时间:2014-12-26 03:42:33

标签: node.js express socket.io

在我的html文件中,我写这个:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Socket.io - System Monitor</title>
    <script src="javascripts/jquery.min.js" type='text/javascript'></script>

</head>
<body>
<h1>Socket.io - System Monitor</h1>
<script src="/socket.io/socket.io.js" type='text/javascript'></script>
<script src="/javascripts/monitor.js" type='text/javascript'></script>

<div id="node">
    <input type="text" id="host" name="host" value="http://localhost"/>
    <input type="button" onclick="startup()" value="OK"/>
</div>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

<script src="/javascripts/highcharts/highcharts.js" type='text/javascript'></script>
<script src="/javascripts/highcharts/modules/exporting.js" type='text/javascript'></script>
</body>
</html>

在服务器端,

var app = express();
// starting http  servers
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

server.listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

我认为每件事情都是正确的,但对于“/socket.io/socket.io.js”来说这是一个错误404 not found

1 个答案:

答案 0 :(得分:0)

尝试使用此代码。

var express = require('express'),
app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);

server.listen(8080);