问题
I have an Angular/NodeJS app, with Socket.io support added in for some real-time features.
I wanted to add MongoDB and PassportJS support so I have migrated to the generator-angular-fullstack structure.
Suddenly, the Socket.io features don't work anymore.
One error I've found is that the client-side JS library served by Socket.io at http://localhost/socket.io/socket.io.js
now returns the index.html page from my app instead.
That sounds like a routing problem, so here is my routing configurations:
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');
}
});
});
Any ideas why the socket.io route is broken?
UPDATE: entire 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;
回答1:
Socket.io is listening to server
with var io = socketio.listen(server);
.
app
is listening to the port, not server
.
change
app.listen(config.port, function () {
console.log('Express server running on http://localhost:%d in %s mode', config.port,
app.get('env'));
});
to
server.listen(config.port, function () {
console.log('Express server running on http://localhost:%d in %s mode', config.port, app.get('env'));
});
来源:https://stackoverflow.com/questions/21173182/why-isnt-my-angularjs-express-socket-io-app-serving-the-socket-io-js-file