Hello everyone,
I’m encountering intermittent H12 errors when using my API proxy server on Heroku, and would appreciate any guidance to resolve this issue.
I have developed an API proxy server using Express and deployed it on Heroku. The server acts as an intermediary between my Android app and the OpenAI API for chatbot functionality. However, I’m experiencing intermittent H12 errors, indicating a request timeout about 10 to 15% of the time. Image Below
I have thoroughly reviewed Heroku’s documentation on troubleshooting H12 errors, followed the recommended steps such as adjusting the timeout settings, checking network connectivity, and optimizing the code, but the errors persist.
I would greatly appreciate any recommendations or suggestions from the community.
I’m open to exploring alternative proxy server setups if that might help mitigate the H12 errors. Below is my current code configuration:
index.js
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
const API_KEY = process.env.OPENAI_API_KEY;
app.use(cors());
app.use(bodyParser.json());
// Increase the timeout for the Axios requests
const instance = axios.create({
timeout: 60000, // Set the timeout to 60 seconds (adjust as needed)
});
app.post('/chat', async (req, res) => {
const requestBody = req.body;
requestBody['temperature'] = 0.7;
requestBody['max_tokens'] = 360;
try {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
requestBody,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${API_KEY}`,
},
}
);
res.json(response.data);
} catch (error) {
console.error('Error:', error);
res.status(500).json({ message: 'An error occurred while processing your request.' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
And here is my package.json
file:
{
"name": "chatgptapiproxy",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.4.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2"
}
}
If anyone has encountered similar issues with API proxy servers on Heroku or has any insights into troubleshooting H12 errors, I would greatly appreciate your assistance and recommendations.
Thank you so much everyone
Rick