I am trying to make api call through Redux actions & reducers in my React Application.
However, I am getting this CORS issue on my browser.
I was wondering if i can resolve this issue from a client side as i donot have any access to the API internally.
Could anyone please help me solve this issue?
Here is the code for newsActions.js :
import * as types from './actionTypes';
const API_KEY = "<Redacted>";
export const getNewsApi = () => {
debugger;
return (dispatch, getState) => {
dispatch({
type: 'API_REQUEST',
options: {
method: 'GET',
service: 'news',
mode: 'cors',
headers:{
'Access-Control-Allow-Origin':'*'
},
endpoint: `https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=${API_KEY}`,
actionTypes: {
success: types.GET_NEWS_API_SUCCESS,
error: types.GET_NEWS_API_ERROR
}
}
});
}
}
Here is the code for server.js :
var express = require('express');
var app = express();
var cors = require('cors');
let __homeglobals = [];
app.use(cors());
app.set("jsonp callback", true);
var server = app.listen(8082, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
If you need additional info or have questions, please comment them below.
Thank you.