Express Production Setup - 3 | CORS To enable Cross-Origin Resource Sharing (CORS) in an Express.js application, you can use the cors middleware package. This middleware allows you to control which domains can access your API, what methods are allowed, and what headers can be sent in requests. index.js const express = require (' express '); const cors = require (' cors '); const app = express (); // CORS options to allow only POST requests const corsOptions = { origin: ' http://example.com ', // Replace with your client's origin methods: [' POST ', " GET ", " PUT ", " PATCH ", " DELETE "], credentials: true // allow cookies }; // Apply CORS middleware with the above options app . use ( cors ( corsOptions )); // Example route app . post (' /a...
Comments
Post a Comment