3

I'm getting this error "NetworkError: 404 Not Found - http://localhost:3000/socket.io/socket.io.js" and I don't know how can I fix it. I have tried changing the port, adding the link with the port ... and I have no idea.

My 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)

note: The console said it's ok ( info - socket.io started )

Anyway I have tried this

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

but doesn't print the port, say me undefined

4

1 回答 1

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);
于 2013-07-19T13:13:10.723 回答