带有快速静态路径连接的Socket.io问题

时间:2015-05-29 08:54:16

标签: node.js sockets express socket.io

我正在尝试将socket.io配置为节点应用程序,但我有一个明确的静态路径:

app.use(express.static(path.join(__dirname, 'public')));

在客户端,socket.io请求如下所示:

var socket = io.connect('http://MyIP:6969/lanmode');

在标题上,我有声明提取CDN的脚本链接。

生成错误404,找不到/socket.io的目录,而他的位置位于 /node_modules/socket.io / 和我的公共'路径是:/ public /所以我无法访问套接字文件夹。

我的server.js文件如下所示:

// server.js

// set up ======================================================================
// get all the tools we need
//var $ = require('jquery'),
//    XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;



var path = require('path'),
    fs = require('fs');
var express  = require('express');
var app      = express();
var port     = process.env.PORT || 6969;
var mongoose = require('mongodb');
var mongoose = require('mongoose');
var passport = require('passport');
var flash    = require('connect-flash');

var morgan       = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser   = require('body-parser');
var session      = require('express-session');

var configDB = require('./config/database.js');


var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

// configuration ===============================================================
mongoose.connect(configDB.url); // connect to our database

require('./config/passport')(passport); // pass passport for configuration

// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser()); // get information from html forms
app.use(express.static(path.join(__dirname, 'public')));//Recursos necessaris tals com imatges, css, js etc

app.set('view engine', 'ejs'); // set up ejs for templating

// required for passport
app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

// routes ======================================================================
require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

// launch ======================================================================
app.listen(port);
console.log('Visiteu la ip corresponent amb port ' + port +' per a visualitzar el projecte');
任何人都可以帮忙吗?感谢

1 个答案:

答案 0 :(得分:1)

如果是Express和socket.io的组合,则必须使http.Server实例(不是Express app对象)成为侦听器才能启用以...开头的socket.io拦截请求/socket.io

所以尝试替换

app.listen(port);

使用:

http.listen(port);

docs

中所述