我在grunt-express上遇到socket.io的问题。
我尝试了很多方法,但它仍然没有用。当我使用socket.io运行express而没有grunt时,我没有遇到这个问题。我在server.js和index.html中有相同的代码。
浏览器控制台出错:
GET http://localhost:9000/socket.io/socket.io.js
angular.js:11655 ReferenceError: io is not defined
at new <anonymous> (http://localhost:9000/scripts/controller.js:13:18)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4203:17)
at $get.extend.instance (http://localhost:9000/bower_components/angular/angular.js:8493:21)
at http://localhost:9000/bower_components/angular/angular.js:7739:13
at forEach (http://localhost:9000/bower_components/angular/angular.js:331:20)
at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7738:11)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7117:13)
at publicLinkFn (http://localhost:9000/bower_components/angular/angular.js:6996:30)
at compile (http://localhost:9000/bower_components/angular-ui-router/release/angular-ui-router.js:3905:9)
at invokeLinkFn (http://localhost:9000/bower_components/angular/angular.js:8258:9) <div class="pages ng-scope" ui-view="">
这是来自server.js的代码:
var express = require('express');
var path = require('path');
var http = require('http');
var io = require('socket.io')(http);
var app = express();
app.use(express.static(path.join(__dirname, './app/')));
index.html中的:
<script src="/socket.io/socket.io.js"></script>
脚本/ controller.js
app.factory('socket', function() {
var socket = io();
return socket;
})
app.controller('loginCtrl', function($scope, socket) {
...
});
和我的Gruntfile.js:
connect: {
options: {
port: 9000,
socketio: true,
open: true,
livereload: 35729,
// Change this to '0.0.0.0' to access the server from outside
hostname: 'localhost'
},
...
express: {
options: {
port: 3000,
delay: 500,
background: true
},
dev: {
options: {
script: '<%= config.app %>/server/server.js'
}
}
},