utkarsh tyagi utkarsh tyagi - 1 year ago 75
Node.js Question

making a call to node.js server

I am learning MEAN stack, so in this case, i am making a call to node.js server


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

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

app.get('/', function(req, res){
res.send("Received the request");

app.use(express.static(__dirname + '/'));
app.listen(process.env.PORT || 5000);

Hosted the server on : https://todoserver.herokuapp.com/

So when i am making a call from angular js.

Angular js service:

TodoService.$inject = ['$http','ApiBasePath'];
function TodoService($http,ApiBasePath){
var service = this;
service.getItems = function(){
var response = $http({
method: "GET",
url: (ApiBasePath + "/"),

service.viewItems = function(){
return items;

ApiBasePath = https://todoserver.herokuapp.com

Hosted on: https://utkarsh17ife.github.io/todo/

I am receiving the response from server like this:

error: function(a)

Which should be like this:

data:"Received the request"

Which is present i go in d>$$state>Value.
I think because of some reason i am not getting the response on correct level. Please tell me what i should do to get the correct reponse object.

Thanks in advance

Answer Source

The $http service returns a promise, you can find the docs here: https://docs.angularjs.org/api/ng/service/$http. You are logging the promise.

You need to chain a success callback in order to log the actual response, as described here: https://docs.angularjs.org/api/ng/service/$http#general-usage

 // Simple GET request example:
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download