Hi, i have built a node JS express API function
it has a post method but when I send the request i get an error
thanks
You'll need to handle the CORS request in the function.
Here's an example for gen1:
https://cloud.google.com/functions/docs/samples/functions-http-cors-auth
and another for gen2:
https://cloud.google.com/functions/docs/samples/functions-http-cors#functions_http_cors-nodejs
These samples won't advise as to the specific constraints you'll want (i.e. the Access-Control-Allow-Origin header), but for that you can just refer to general CORS guidance:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
Hi @SnowRanger11,
Welcome to Google Cloud Community!
npm install cors
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
// Your routes go here
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
app.use(cors({
origin: 'http://example.com'
}));
const { cors } = require('@google-cloud/functions-framework');
exports.myFunction = cors((req, res) => {
// Your function logic goes here
});
const { cors } = require('@google-cloud/functions-framework');
exports.myFunction = cors((req, res) => {
// Your function logic goes here
}, {
origin: 'http://example.com'
});