Marc
Marc

Reputation: 137

Angular+Socket.io Handler

I am trying to understand Angular.js and Socket.io. But it seems that my node.js/socket.io does not function as a real webserver with routing to angular.js and controller.js. Shouldnt that be the task of "handler"? My handler looks like this and ONLY returns the page.html. How can I make it also route to /lib/angular.js and /js/controller.js ? I feel very stupid since I dont get the technology.

var handler = function(req, res) {
fs.readFile('page.html', function(err, data) {
    if (err)
        throw err;
    res.writeHead(200);
    res.end(data);
});

my page.html looks like this:

<html ng-app="MyTestApp">...
    <script src="./lib/angular/angular.js"></script>
    <script type="text/javascript" src="./js/controllers.js"></script>...
<body ng-controller="userController">
    <table>
        <thead>
            <tr><th colspan="4">All online users</th></tr>
        </thead>
        <tbody>
            <tr ng-repeat="user in UserList">
                <td>{{$index + 1}}</td>
                    <td>
                    <img src="img/flags/{{nationality}}.png" />
                    {{user.Driver.givenName}}
                </td>
            </tr>
        </tbody>
        </table>

And my controller.js:

angular.module('MyTestApp', [
    'MyTestApp.controllers'
 ]);

angular.module('MyTestApp.controllers', []).
    controller('userController', function($scope) {
$scope.UserList = [
  { Driver: {
        givenName: 'Sebastian'
        },
    nationality: "German"
  },
  { Driver: {
        givenName: 'Fernando'
        },
    nationality: "Spanish",
  }
];});

So its just a simple test but in my page.html I dont get the right data and I thought, that angular.js is not loaded by node.js?:

 {{$index + 1}}...{{user.Driver.givenName}}

Thanks, guys.

Upvotes: 0

Views: 157

Answers (2)

Marc
Marc

Reputation: 137

John was totally right. I got it to work with express This is the code:

var express = require('express');
var app = express();
app.configure(function() {
    app.use(express.static(__dirname+'/public'));
});
var server = require('http').createServer(app).listen(1337);
var io = require('socket.io').listen(server);

Upvotes: 1

John
John

Reputation: 746

You should use (http://expressjs.com) and use that to serve up page.html and your JS assets. Should take something like 5 lines.

Upvotes: 1

Related Questions