0

I have made an api end point using express.

Now im trying to get the data from my localhost server that is running on port 3010.

$scope.getBooks = function(){
$http.get('/api/books').then(function(resp){
  $scope.books = resp;
});
}

The function is working good because i can test whit the JSONPlaceholder.com.

I cannot get my data, im using gulp on port 3002 and my server is in port 3010, he is running and working good, i can see my data using postman.

why can i get my data from the localhost server ?

Thank You

10
  • did you try to use postman on localhost:3002/api/books? you might need to configure a proxy, redirecting any /api/** request to port 3010. What gulp webserver are you using? Commented Mar 31, 2018 at 20:10
  • Hello Oliver, if i do that on postman i get Cannot GET /api/books, im not using any gulp webserver, i am using the express, im only using gulp for the front end structure not for server side Commented Mar 31, 2018 at 20:13
  • As for the server side im using express and mongoose Commented Mar 31, 2018 at 20:14
  • But how are you serving your frontend on port 3002? Commented Mar 31, 2018 at 20:14
  • @Oliver, whit gulp, i dont know how to change that, he automatically choose 3002 Commented Mar 31, 2018 at 20:16

5 Answers 5

1

This issue happens because the API endpoint you are accessing does not implement CORS. If you run your code in Chrome and look at the console, you will see an error:

XMLHttpRequest cannot load .....

The solution is to change the API endpoint to set the Access-Control-Allow-Origin header to either the wildcard * or to the domain where the page using the JavaScript code will be served from.

For More Details on CORS

To prevent websites from tampering with each other, web browsers implement a security measure known as the same-origin policy. The same-origin policy lets resources (such as JavaScript) interact with resources from the same domain, but not with resources from a different domain. This provides security for the user by preventing abuse, such as running a script that reads the password field on a secure website.

Sign up to request clarification or add additional context in comments.

Comments

1

If your site is on port 3002 and your server is on port 3010 then you must specify the URL of the entire server's location

$scope.getBooks = function(){
    $http.get('http://localhost:3010/api/books').then(function(resp){
        $scope.books = resp;
    });
}

4 Comments

hello @sean , i have done that i got Failed to load localhost:3010/api/books: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:3002' is therefore not allowed access.
then you have to make your server add the Access-Control-Allow-Origin: * header to the responses
if i put that url on my browser i can see my books, so the url is working nice
Isn't it case of CORS ? does your API hosted on port 3010 has CORS enabled ?
1

Specify the entire url in AngularJS code and use CORS middleware in the express backend.

https://github.com/expressjs/cors

Comments

1

This is bad way of doing front end(AngularJs) and backend development parallely on local on same machine. Try to fit Front-end repository to be hosted by BE server and use it in following way :

If you are doing FE development locally and backend server is also hosted locally.

use following line as common baseUrl

var baseUrl = "//" + window.location.host +'/'

Doing so, you don't need to update while committing changes to prod environment.

 $scope.getBooks = function(){
    var _url = baseUrl +  '/api/books'
    $http.get(_url).then(function(resp){
      $scope.books = resp;
    });
}

Above code works in case of same server FE and BE.

If you have different servers locally , you need to work more with setting :

You can use express-http-proxy

var proxy = require('express-http-proxy');

app.use('/api/', proxy('http://localhost:3010'));

In higher of version of angular 4+ , we have such setting as in initial configuration.

READ ARTICLE :
https://juristr.com/blog/2016/11/configure-proxy-api-angular-cli/

3 Comments

can you help me return this response to the front end without need to click ?
if i do <div ng-click="getBooks()" class="home-page"> {{books}} </div> it works but i dont want to click
Use self provoking function to call getBooks method
0

You cannot just write /api/books. You need to specify your port number of API running on localhost. In your case localhost:3010/api/books

7 Comments

Hello Vishes, i got an error like this if i do that Failed to load localhost:3010/api/books: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https
Did you specified your api path in index file in frontend?
What ? sorry i dont understand
You have to specify you API port number in front end. And by front end means there would be a main file in gulp from where it runs. so there you have to specify it that you API is running on this port.
I was looking at browserSync options !!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.