express.js
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.use(express.static(path.join(__dirname, 'doc')));
app.use(express.json({ limit: "10mb", type: "application/json" }));
app.use(cors())
app.use('/search', proxy({
pathRewrite: {
'^/search': '/search'
},
target:<target API URL>,
secure: false
}));
const httpServer = http.createServer(app);
httpServer.listen(HTTP_PORT);
Axios call
getBacklogItemsData = (query) => {
const jqlQuery = this.getBacklogItemsJQLQuery(query);
const headers = {
'Content-Type': 'application/json',
'Authorization': "Basic <authString>",
'Access-Control-Allow-Origin': '*',
};
const auth = {
username: <id>,
password: <pass>,
}
const proxy = {
host: 'localhost',
port: 5000
};
let config = {
headers
}
return axios.get(`/search?jql=${jqlQuery}/`, auth, headers, proxy, config);
}
package.json
"proxy": "https://localhost:5000",
"homepage": ".",
What have I tried so far?
- Direct call to the API with the same requested parameters returns the correct response (using postman and web browser)
- Added "." as homepage in package.json
- Added localhost:5000 as proxy in package.json
- Used corsOption -
var corsOptions = {
origin: 'http://localhost:5000',
optionsSuccessStatus: 200
}
app.use('/search', cors(corsOptions), proxy({
pathRewrite: {
'^/search': '/search'
},
target: baseURL,
secure: false
}), function (req, res, next) {
res.json({msg: 'This is CORS-enabled for only example.com.'})
});
- I did a direct axios request to target API and got CORS issue
axios.get(`<TargetAPIUrl>/search?jql=${jqlQuery}/`, auth, headers, proxy, config);
- Added proxy parameters to axios.get call as shown above
<noscript>
tags. I'm not sure how it would appear in an API response. – Barmar