I am trying to add cors to my PostGraphile route in an express server.
app.use(
postgraphile(process.env.DATABASE_URL || process.env.POSTGRES, ‘public’, {
watchPg: true,
graphiql: true,
enhanceGraphiql: true,
enableCors: true,
}),
);
When I am trying to call the route from postmen I get * (wildcard) in Access-Control-Allow-Origin.
How do I add specific routes to return by Access-Control-Allow-Origin?
you can use the cors middleware before the postgraphile and set the origin in the options:
const cors = require('cors');
const { postgraphile } = require('postgraphile');
const options = {
origin: 'https://your_origin',
};
app.use(cors(options));
// Enable pre-flight requests for all routes
app.options('*', cors(options));
app.use(
postgraphile(process.env.DATABASE_URL || process.env.POSTGRES, ‘public’, {
watchPg: true,
graphiql: true,
enhanceGraphiql: true,
}),
);
this will set the Access-Control-Allow-Origin
header to https://your_origin