gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:49 25 4
gpt4 key购买 nike

我有一个 Angular/NodeJS 应用程序,添加了 Socket.io 支持以实现一些实时功能。

我想添加 MongoDB 和 PassportJS 支持,因此我已迁移到 generator-angular-fullstack结构。

突然之间,Socket.io 功能不再起作用。

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

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

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); 监听 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'));
});

关于node.js - 为什么我的 AngularJS/Express/Socket.io 应用程序不提供 socket.io.js 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21173182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com