Socket.io - ReferenceError: io is not defined on Grunt-express

I have a problem with socket.io on grunt-express.

I tried many ways but it still doesn't work. I didn't have this problem when I ran express with socket.io without a grunt. I had the same code in server.js and index.html.

There is an error in the browser console:

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=""> 

      

Here is my code from 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/')));

      

in index.html:

<script src="/socket.io/socket.io.js"></script>

      

scripts / controller.js

app.factory('socket', function() {
    var socket = io();
    return socket;
})

app.controller('loginCtrl', function($scope, socket) {
    ...
});

      

and my 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'
      }
    }
},

      

+3


source to share





All Articles