I am sending a POST request to the URL shown in the picture, but I’m encountering a CORS error. However, I have configured it to allow any address. Everything worked fine when running locally, but I am having issues after deployment.
const functions = require('firebase-functions');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const OpenAI = require('openai');
const app = express();
app.use(cors({ origin: true }));
app.use(bodyParser.json());
const OPENAI_API_KEY = functions.config().openai.key;
const client = new OpenAI({
apiKey: OPENAI_API_KEY,
});
app.post('/api', async (req, res) => {
const { name } = req.body;
if (!name) {
console.error('Name is missing from request body');
return res.status(400).json({ error: 'Name is required' });
}
try {
console.log(`Request received with name: ${name}`);
const response = await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: `Translate this name to Korean: ${name}` }],
});
console.log('OpenAI API response:', response);
const translatedName = response.choices[0].message.content.trim();
console.log(`Translated name: ${translatedName}`);
return res.json({ translatedName });
} catch (error) {
console.error('Error communicating with OpenAI:', error.response?.data || error.message || error);
return res.status(500).json({ error: 'Failed to translate name' });
}
});
exports.api = functions.https.onRequest(app);
Request URL:
https://us-central1-nulligpt.cloudfunctions.net/api
Referrer Policy:
strict-origin-when-cross-origin
content-type:
application/json; charset=utf-8
referer:
https://nulligpt.web.app/
sec-ch-ua:
“Google Chrome”;v=”129″, “Not=A?Brand”;v=”8″, “Chromium”;v=”129″
sec-ch-ua-mobile:
?0
sec-ch-ua-platform:
“Windows”
user-agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36