根据文档,我有以下设置:
客户端:
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect(); // TIP: .connect with no args does auto-discovery
服务器端:
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path');
app = express(),
server = http.createServer(app).listen(80),
io = require('socket.io').listen(server);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
为什么我
GET http://localhost:5000/socket.io/socket.io.js 404 (Not Found)
我尝试切换到server = http.createServer(app).listen(process.env.PORT || 3000),
然后我得到了warn: error raised: Error: listen EACCES
和相同的404.我在这里想到的端口用法是什么?
答案 0 :(得分:8)
如果没有特殊权限,则无法在端口80上创建服务器。在另一个旁注中,您正在尝试创建2个服务器(一个在端口80上,另一个在端口3000上)。
这是我正在为项目使用的(部分):
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, sio = require('socket.io').listen(server)
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
// configure your socket.io instance here
server.listen(app.get('port'), function() {
// server started
});