为什么我的AngularJS / Express / Socket.io应用程序不能提供socket.io.js文件?

我有一个Angular / NodeJS应用程序,join了一些实时function的Socket.io支持。

我想添加MongoDB和PassportJS支持,所以我已经迁移到了generator-angular-fullstack结构。

突然间,Socket.iofunction不再工作了。

我发现的一个错误是由http://localhost/socket.io/socket.io.js的Socket.io提供的客户端JS库现在返回的是我的应用程序中的index.html页面。

这听起来像一个路由问题,所以这里是我的路由configuration:

lib / routes.js(NodeJS):

 module.exports = function(app) { // Server API Routes app.get('/api/awesomeThings', api.awesomeThings); app.post('/api/users', users.create); app.put('/api/users', users.changePassword); app.get('/api/users/me', users.me); app.get('/api/users/:id', users.show); app.post('/api/session', session.login); app.del('/api/session', session.logout); // All other routes to use Angular routing in app/scripts/app.js app.get('/partials/*', index.partials); app.get('/*', middleware.setUserCookie, index.index); app.get('/:session', function(req, res) { res.render('views/index.html', { title: 'Weld Spark' }); }); }; 

app / scripts / app.js(AngularJS):

 angular.module('weld.common').config(function($routeProvider, $locationProvider, $httpProvider) { $routeProvider .when('/main', { templateUrl: 'partials/main', controller: 'MainCtrl' }) .when('/login', { templateUrl: 'partials/login', controller: 'LoginCtrl' }) .when('/signup', { templateUrl: 'partials/signup', controller: 'SignupCtrl' }) .when('/settings', { templateUrl: 'partials/settings', controller: 'SettingsCtrl', authenticate: true }) .when('/:session', { templateUrl: 'views/ui-editor/editor.html', controller: 'weldEditorController' }) .otherwise({ redirectTo: '/my-project' }); $locationProvider.html5Mode(true).hashPrefix('!'); // TODO: Test and figure out how this works in IE // Intercept 401s and 403s and redirect you to login $httpProvider.interceptors.push(['$q', '$location', function($q, $location) { return { 'responseError': function(response) { if(response.status === 401 || response.status === 403) { $location.path('/login'); return $q.reject(response); } else { return $q.reject(response); } } }; }]); }) .run(function ($rootScope, $location, Auth) { // Redirect to login if route requires auth and you're not logged in $rootScope.$on('$routeChangeStart', function (event, next) { if (next.authenticate && !Auth.isLoggedIn()) { $location.path('/login'); } }); }); 

任何想法为什么socket.io路由被破坏?

更新:整个server.js:

 // Init Express framework and Socket.io var express = require('express'), path = require('path'), fs = require('fs'), mongoose = require('mongoose'), http = require('http'), socketio = require('socket.io'); process.env.NODE_ENV = process.env.NODE_ENV || 'development'; // Application Config var config = require('./lib/config/config'); // Connect to database var db = mongoose.connect(config.mongo.uri, config.mongo.options); // Bootstrap models var modelsPath = path.join(__dirname, 'lib/models'); fs.readdirSync(modelsPath).forEach(function (file) { require(modelsPath + '/' + file); }); // Populate empty database with sample data require('./lib/config/dummydata'); // Passport Configuration require('./lib/config/passport')(); // Init app and web server var app = express(); var server = http.createServer(app); // Init Socket.io server var io = socketio.listen(server); app.set('socket.io.log.level', process.env.SOCKET_IO_LOG_LEVEL || 1); require('./lib/socket')(io, app.get('socket.io.log.level')); // Heroku web socket workaround // https://devcenter.heroku.com/articles/getting-started-with-nodejs // https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku io.configure(function() { io.set('transports', ['xhr-polling']); io.set('polling duration', 10); }); // Express settings require('./lib/config/express')(app); // Routing require('./lib/routes')(app); // Web: Start server app.listen(config.port, function () { console.log('Express server running on http://localhost:%d in %s mode', config.port, app.get('env')); }); // Expose app exports = module.exports = app; 

Socket.io使用var io = socketio.listen(server);来监听var io = socketio.listen(server);

app正在监听端口,而不是server

更改

 app.listen(config.port, function () { console.log('Express server running on http://localhost:%d in %s mode', config.port, app.get('env')); }); 

 server.listen(config.port, function () { console.log('Express server running on http://localhost:%d in %s mode', config.port, app.get('env')); });