So my express server is a middleman between a front-end app and an API that doesn't accept requests directly from the browser.
If I call the API from postman, I get a successful response. For context, heres the Postman code (in HTML view):
POST hosted3.xyz.com/lookup\ HTTP/1.1
Content-Type: application/json
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Accept-Encoding: gzip, deflate
Host: https://hosted3.xyz.com
Cache-Control: no-cache
Postman-Token: 72cdc54c-777f-e385-f6b8-5a3c5a8c0371
{body...}
This works fine.
Now my express server is at localhost:4041 and its set up to make the same call as shown above, but this fails with a 500 Error: read ECONNRESET message.
Here is what that code looks like:
const express = require("express");
const bodyParser = require('body-parser');
const axios = require ('axios');
const xmlParser = require('fast-xml-parser');
const he = require('he');
const cors = require('cors')
const app = express();
const port = process.env.PORT || 4041;
app.use(cors())
app.options('*', cors());
app.post ('/querywrapper', (req, res, next) => {
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json, text/json, text/x-json, text/javascript, application/xml, text/xml',
'Accept-Encoding': 'gzip, deflate',
'Host': 'https://hosted3.xyz.com'
};
console.log (req.body);
try {
axios.post('https://hosted3.xyz.com/lookup/', req.body, { headers })
.then(response => {
console.log ("recieved")
console.log(JSON.stringify(response));
res.json(response);
})
.catch(err =>
next(err));
}
catch{ }
});
I'm testing this by running it and making the call (same body) from Postman. So I know it has nothing to do with my front end.
What am I doing wrong? And in with errors like these, what is the best way to debug?
Thanks!