News-API-gh / News-API-node

Our officially supported Node client library for accessing News API.
MIT License
43 stars 3 forks source link

Throwing Cros on Angular UNIVERSAL . #1

Open kakul232 opened 6 years ago

kakul232 commented 6 years ago

Hi , Its Works perfetly on Angular 5 , But when i switch it to universal its throwing

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://newsapi.org/v2/top-headlines?country=in&category=technology&pageSize=5. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Unhandled rejection 9jcD/</h.prototype._onFinish/d.onerror@http://localhost:4000/main.5b638d685502c047f09e.bundle.js:1:230171
D@http://localhost:4000/polyfills.2749d91dc0f503fb6b5e.bundle.js:1:35067
eFQL/</</</c</t.prototype.invokeTask@http://localhost:4000/polyfills.2749d91dc0f503fb6b5e.bundle.js:1:26386
onInvokeTask@http://localhost:4000/main.5b638d685502c047f09e.bundle.js:1:558505
eFQL/</</</c</t.prototype.invokeTask@http://localhost:4000/polyfills.2749d91dc0f503fb6b5e.bundle.js:1:26299
eFQL/</</</o</e.prototype.runTask@http://localhost:4000/polyfills.2749d91dc0f503fb6b5e.bundle.js:1:21556
eFQL/</</</u</e.invokeTask@http://localhost:4000/polyfills.2749d91dc0f503fb6b5e.bundle.js:1:27480
_@http://localhost:4000/polyfills.2749d91dc0f503fb6b5e.bundle.js:1:38181
k@http://localhost:4000/polyfills.2749d91dc0f503fb6b5e.bundle.js:1:38413

I have setting up my server.ts with this following code but not fixed yet

app.use(function (req, res, next) {

  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', '*');

  // Request methods you wish to allow
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,accept');

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true);

  // Pass to next layer of middleware
  next();
});