http.get problems angular + nodejs

When i use this code, work and i get tem response "ok":

controller.js

    var myApp = angular.module('myApp',[]);

myApp.controller('AppController',['$scope','$http',function($scope,$http){

    $http.get('/').
        success(function (response) {
        console.log("ok");
    });

}]);

server.js

var express = require('express');
var app = express();

app.use(express.static(__dirname + "/public"));

app.get('/', function (req, res){

    var person1 = {
        name: 'Tim',
        email:'[email protected]',
        number:'32232233'
    };

    var person2 = {
        name :'Dani',
        email:'[email protected]',
        number:'22222222'
    };

    var contactList = [person1,person2];

    res.json = contactList;
    console.log("send work");
});

app.listen(3000);
console.log("Server running on port 3000");

but, when i change $http.get('/') to $http.get('/list') and app.get('/', function (req, res) to app.get('/list', function (req, res) not works.

The get time 'list' stay Pending for ever, and when i stop node server i get "GET http://localhost:3000/list net::ERR_EMPTY_RESPONSE".

Upvotes: 1

Views: 890

Answers (1)

Brian Noah
Brian Noah

Reputation: 2972

You aren't actually calling the res.json() method.

Make sure you are actually invoking the method rather than assigning it to equal the contact list:

res.json(contactList);

vs.

res.json = contactList

In context:

app.get('/list', function (req, res){

    var person1 = {
        name: 'Tim',
        email:'[email protected]',
        number:'32232233'
    };

    var person2 = {
        name :'Dani',
        email:'[email protected]',
        number:'22222222'
    };

    var contactList = [person1,person2];

    res.json(contactList);
    console.log("send work");
});

The reason you are probably getting a response on '/' is because your app is hosted on '/', so you are going to get a response.

Upvotes: 2

Related Questions