2

根据文档,我有以下设置:

客户端:

<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),bu,然后得到warn: error raised: Error: listen EACCES相同的 404。关于端口使用情况,我在这里遗漏了什么?

4

1 回答 1

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
});
于 2013-01-16T08:11:11.863 回答