CORS error nextjs firbase functions
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
Hello guys, I'm using a firebase function to run my api on and I get CORS error even though CORS is being configured, this is my code:
error:
import * as cors from "cors";
const corsHandler = cors({ origin: true });
exports.extractText = functions
.runWith({ timeoutSeconds: 360 })
.https.onRequest(async (req, res) => {
corsHandler(req, res, async () => {
res.status(200).json({ extractedText: "Hello from Firebase!" });
});
});error:
11 Replies
Chilean jack mackerel
Questions for you.
Did you get the CORS error in your local or production?
I can fix the CORS error in any Next.js app.
I spent a lots of time to fix that. but finally, I found it.
const proxy = require("cors-anywhere");
const PORT = process.env.PORT || 8080;
proxy
.createServer({
originWhitelist: [], // Allow all origins
requireHeader: ["origin", "x-requested-with"], // Add any additional headers required by the API
removeHeaders: ["cookie", "cookie2"], // Remove any sensitive headers
})
.listen(PORT, () => {
console.log(
});
const PORT = process.env.PORT || 8080;
proxy
.createServer({
originWhitelist: [], // Allow all origins
requireHeader: ["origin", "x-requested-with"], // Add any additional headers required by the API
removeHeaders: ["cookie", "cookie2"], // Remove any sensitive headers
})
.listen(PORT, () => {
console.log(
Proxy server running on http://localhost:${PORT});});
That's perfect CORS solution in any local dev.
save the code with named server.js in root directory or Next project.
and run node server.js
and next run dev.
and next run dev.
That's all.
Orange-tailed bumble bee
Thanks a lot @Chilean jack mackerel
This save a lot of time.
Will you help me in production as well?
This save a lot of time.
Will you help me in production as well?
Chilean jack mackerel
Okay