0

Currently I built API server at HTTPS together with mobile and website. The front-ends mobile(ionic web/mobile) with port 8100 is working with the API HTTPS. But when it comes to angular web with port 9999, the API HTTPS doesn't work so well, but running API server at localhost:8888 work like a charm.

ExpressJS

var cors = require('cors');
var http = require('http');
var app = express();
var server = http.createServer(app);
var port = process.env.PORT || 3000; // Always running at port 8888
app.use(cors());

Request Header

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Authorization: (Some long string here)
Cache-Control:max-age=0
Connection:keep-alive
Host: api.web.com
Origin: http://localhost:9999

Problem

I don't have issue using POST API to server before user authorization. After I stored access token on Authorization header and direct to dashboard, every methods of API calls (GET,PUT,POST) returns 400 BAD REQUEST.

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9999' is therefore not allowed access. The response had HTTP status code 400.

Foonation
  • 215
  • 3
  • 17
  • It seems you are using 'http' module. Not 'https'. – Rayee Roded Nov 18 '16 at 00:06
  • Possible duplicate: http://stackoverflow.com/questions/18310394/no-access-control-allow-origin-node-apache-port-issue – Rayee Roded Nov 18 '16 at 00:10
  • You are CORS and per the rules of CORS, a different port is a different origin, as far as it's concerned. As the answer from the link provided by @RLaaa shows, you'll need to set a response header for `Access-Control-Allow-Origin`. Valid [values for this header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin) can be `*` for all or a specific domain. – Eric McCormick Nov 18 '16 at 03:48

0 Answers0