What is @types/cors?
The @types/cors package provides TypeScript type definitions for the cors middleware used in Express/Connect applications. It allows TypeScript developers to use cors in their projects with the benefits of type checking and IntelliSense in their IDE. This package does not contain functionality by itself but offers type definitions to enhance development experience when using the cors package.
CORS Configuration
This code sample demonstrates how to configure CORS policy for an Express application. It specifies that only requests from 'http://example.com' are allowed and sets a successful status code for preflight requests.
{ "origin": "http://example.com", "optionsSuccessStatus": 200 }
Enabling CORS for a Single Route
This example shows how to enable CORS for a specific route in an Express application. It uses the cors middleware directly in the route definition.
app.get('/products/:id', cors(), function (req, res, next) { res.json({msg: 'This is CORS-enabled for a Single Route'}); })