InterviewHubPRO
Section Overview
Q43

Question 43 of 60

Part 4: Express, Fastify, APIs & Middleware (36 - 50)

72%
Node.js & Libuv•Part 4: Express, Fastify, APIs & Middleware (36 - 50)

CORS and Preflight OPTIONS requests.

In Simple Words: Preflight OPTIONS requests are sent by browsers for requests with non-simple methods (PUT, DELETE, PATCH) or custom headers (Authorization, Content-Type: application/json). Safe configuration specifies explicit origin whitelist instead of wildcard *.

javascript
const cors = require('cors');

app.use(cors({
  origin: ['https://myapp.com', 'https://admin.myapp.com'],
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  credentials: true
}));