socketio和nodejs,错误404 /socket.io/socket.io.js

时间:2013-07-18 21:15:05

标签: node.js express socket.io pug

我收到此错误“NetworkError:404 Not Found - http://localhost:3000/socket.io/socket.io.js",我不知道如何修复它。我尝试更改端口,添加端口链接......我不知道

我的app.js:

var express = require('express')
  , http = require('http')
  , stylus = require('stylus')
  , nib = require('nib')


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


function compile(str, path) {
  return stylus(str)
    .set('filename', path)
    .use(nib());
}

app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.logger('dev'))
app.use(stylus.middleware(
  { src: __dirname + '/public'
  , compile: compile
  }
))
app.use(express.static(__dirname + '/public'))

app.get('/', function (req, res) {
  res.render('index',
  { title : 'Home' }
  )
})

app.listen(3000)

注意:控制台说没关系(info - socket.io启动)

无论如何我试过这个

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

但不打印端口,说我未定义

1 个答案:

答案 0 :(得分:7)

您正在创建快速HTTP服务器以及标准HTTP服务器。上例中的应用程序是指没有.get('port')的Express应用程序;方法,尝试使用server.listen。例如:

var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

app.get('/', function(req, res) {
    res.send("Hello World!");
});

server.listen(3000);
console.log('Express server started on port %s', server.address().port);