I am sending photos across to an API. Because of the CORS error that I am facing, I made a proxy server that acts as the middle server. My front-end VueJS app will send data to the middle proxy, which then continues to the end server, which is the API.
Below is the server code:
// CAMERA API
app.post('/meitu', (req, res) => {
const myFile = req.files.photo;
console.log(`${__dirname}`)
// mv() method places the file inside public directory
myFile.mv(`./public/tempData/${myFile.name}`, function (err) {
if (err) {
console.log(err)
return res.status(500).send({ msg: "Error has occured" });
}
console.log("Uploading data to server...")
console.log(`./public/tempData/${myFile.name}`)
let data = new FormData();
data.append('photo', fs.createReadStream(`./public/tempData/${myFile.name}`));
// Token, URL, and Cookie are censored for privacy purposes.
let config = {
method: 'post',
maxBodyLength: Infinity,
url: '-------------------------------',
headers: {
'userToken': '--------------------------------------------',
'Cookie': '.AspNetCore.Session=-----------------------------------',
...data.getHeaders()
},
data : data
};
axios.request(config)
.then(response => {
console.log("Data sending complete.")
const result=response.data;
console.log(result)
fs.unlinkSync(`./public/tempData/${myFile.name}`)
return res.send(JSON.stringify(result));
})
.catch((error) => {
console.log(error);
});
})
})
and I get this ERROR:
Nov 7 01:23:04 PM AxiosError: getaddrinfo ENOTFOUND --------------------.com
Nov 7 01:23:04 PM at AxiosError.from (/opt/render/project/src/node_modules/axios/dist/node/axios.cjs:837:14)
Nov 7 01:23:04 PM at RedirectableRequest.handleRequestError (/opt/render/project/src/node_modules/axios/dist/node/axios.cjs:3029:25)
Nov 7 01:23:04 PM at RedirectableRequest.emit (node:events:513:28)
Nov 7 01:23:04 PM at eventHandlers.<computed> (/opt/render/project/src/node_modules/follow-redirects/index.js:14:24)
Nov 7 01:23:04 PM at ClientRequest.emit (node:events:513:28)
Nov 7 01:23:04 PM at TLSSocket.socketErrorListener (node:_http_client:502:9)
Nov 7 01:23:04 PM at TLSSocket.emit (node:events:513:28)
Nov 7 01:23:04 PM at emitErrorNT (node:internal/streams/destroy:151:8)
Nov 7 01:23:04 PM at emitErrorCloseNT (node:internal/streams/destroy:116:3)
Nov 7 01:23:04 PM at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
Nov 7 01:23:04 PM hostname: '--------------------.com',
Nov 7 01:23:04 PM syscall: 'getaddrinfo',
Nov 7 01:23:04 PM code: 'ENOTFOUND',
Nov 7 01:23:04 PM errno: -3008,
.....
Nov 7 01:23:04 PM cause: Error: getaddrinfo ENOTFOUND v<---------->.com // Actual address redacted for privacy
Nov 7 01:23:04 PM at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
Nov 7 01:23:04 PM errno: -3008,
Nov 7 01:23:04 PM code: 'ENOTFOUND',
Nov 7 01:23:04 PM syscall: 'getaddrinfo',
Nov 7 01:23:04 PM hostname: '--------------------.com'
Nov 7 01:23:04 PM }
Nov 7 01:23:04 PM }
Postman works completely fine, and I used the code from Postman, and I get the above error. How to fix this?
can the server resolve the IP of the host you’re sending to it?
are you doing something silly like putting a protocol name on the front of the hostname?
Right, but i’m fairly sure that getAddrInfo is expecting a protocol-less address, at least according to the googling i did.
So if it can’t resolve the IP of the server, how do you expect it to communicate with said server?
If the server address isnt resolvable through normal DNS, you should add it to your system’s hosts resolution file so it can find the server.
Well, asa matter of fact, I do not know whether it can resolve or not. Sometimes I could reach the server, but most of the time the request timed out more frequently. And even with the protocol, I was sometimes lucky.
As @m_hutley said, the ENOTFOUND error typically means that the DNS lookup failed when trying to resolve the hostname into an IP address, which could happen if the hostname is incorrect, the server is not reachable, or there’s an issue with the DNS configuration.
Given that your code works fine in Postman but not when deployed to Render, here are several steps you can take to troubleshoot and potentially resolve this issue:
Double-check the URL: Make sure that the URL you are using in your server code is exactly the same as the one you are using in Postman. A common mistake is to have a typo or missing part in the URL.
DNS Configuration: Check the DNS settings on Render to ensure that they are correct and that the hostname can be resolved from the Render environment. You may also want to try resolving the DNS manually from a terminal in the Render environment, if possible.
HTTP vs. HTTPS: Make sure you’re using the correct protocol (HTTP vs. HTTPS). If the API requires HTTPS and you are using HTTP, or vice versa, the connection will fail.
Environment Variables: If you are using environment variables to store the API URL or tokens, make sure they are set correctly in the Render environment.
Proxy Settings: If Render requires you to use a proxy to make outbound HTTP requests, ensure your code or environment is correctly configured to use this proxy.
Firewall/Network Issues: There could be firewall restrictions or network issues on Render’s side that are preventing the DNS lookup or the connection to the API server. Check if Render has any documentation on outbound requests or contact their support.
Dependencies: Ensure that all your dependencies, particularly axios, are up to date and compatible with the environment where your server is deployed.
Error Handling: Improve error handling in your .catch block to log more detailed information about the error. Sometimes the error object contains additional details that can help you troubleshoot.
.catch((error) => {
if (error.response) {
// Request made and server responded
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
Headers and Body: Ensure the headers and body of the request are formatted correctly, and that they match what the API expects, which you say works correctly when tested with Postman.
Actually yes, the URL that I used is EXACTLY the same as Postman, and the chance of successfully getting data from the API is just 30%, and it’s always HTTPS for me. I’ve switched to Heroku again btw, so there may be other stuff that I might have missed… and if there was firewall issues, then I wouldn’t have gotten anything at all, which wasn’t true. I manage to contact the API, but with such small chance.
Do you have a paid account for this API or is it a costless? How many requests are you sending in a hour/minute/second? As I said, many costless services block if you use it too much.
Heroku account alone is free, it costs $7 (basic plan) to host each app. I probably sent about two or three requests in a minute.
But my previous projects involving API work never had any issues. My earlier version of the project, which separate from the problematic one I have now, works perfectly fine, with no real errors, even if I sent about five-ten requests in a minute. It also ran on the $7 plan on Heorku. The service for the early version is currently suspended.
Both the current project and the early project run on servers hosted by Heroku for $7, and only the newest one had issues.
UPDATE: You are right, @Thallius. The API requests for the early project also timed out on me. Too many requests in a short period of time will clog the server. Thanks for the heads-up.