Configuring CORS

Allowing Specific Origins

If you want to restrict CORS to a specific origin, you can set it as follows:

const corsOptions = {
    // Allow only requests from this domain
    origin: 'http://example.com',
};

app.use(cors(corsOptions));

Allowing Multiple Origins

You can also allow multiple origins by specifying them in an array:

const allowedOrigins = ['http://example.com', 'http://another-domain.com'];

const corsOptions = {
    origin: function (origin, callback) {
        if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
            callback(null, true);
        } else {
            callback(new Error('Not allowed by CORS'));
        }
    },
};

app.use(cors(corsOptions));

Allowing Specific Methods

To allow only specific HTTP methods, use the following configuration:

const corsOptions = {
    // Only allow GET and POST requests
    methods: ['GET', 'POST'],
};

app.use(cors(corsOptions));

Supporting Credentials

If you need to support credentials, you can enable them with this configuration:

const corsOptions = {
    origin: 'http://example.com',
    // Allow credentials like cookies
    credentials: true,
};

app.use(cors(corsOptions));

Customizing Headers

To specify which headers are allowed in CORS requests, use this approach:

const corsOptions = {
    // Allow specific headers
    allowedHeaders: ['Content-Type', 'Authorization'],
};

app.use(cors(corsOptions));

NPM CORS

Cross-Origin Resource Sharing (CORS) is a fundamental security mechanism implemented by web browsers to prevent unauthorized access to resources on a web page from different origins. In Node.js, CORS management is essential when building APIs that need to be consumed by clients running on different domains.

Table of Content

  • What is CORS?
  • Need for CORS in Node.js
  • Using npm Cors Package
  • Basic Usage
  • Features
  • Configuring CORS
  • Conclusion

Similar Reads

What is CORS?

CORS is a security feature implemented by web browsers to restrict web pages from making requests to a different origin than the one from which the current page originated. This is enforced by the Same-Origin Policy, a security concept that restricts how documents or scripts loaded from one origin can interact with resources from another origin....

Need for CORS in Node.js

Node.js applications often serve APIs that need to be consumed by clients running on different origins. Enabling CORS in a Node.js server allows these clients to make requests to the server, even if they originate from different domains. This is crucial for building scalable and interoperable web applications....

Using npm Cors Package

The cors package is a popular middleware for Express.js that simplifies CORS configuration in Node.js applications. It provides a flexible way to configure CORS policies based on specific requirements, making it a go-to solution for managing cross-origin requests....

Basic Usage

To use the cors module, you need to import it and apply it to your Express application. Here’s a simple example of how to enable CORS for all requests in an Express-based application:...

Features

The cors module provides various features to configure cross-origin behavior in Node.js applications:...

Configuring CORS

Allowing Specific Origins...

Conclusion

CORS management is crucial for securing Node.js applications and enabling cross-origin communication between clients and servers. The cors npm package simplifies CORS configuration in Node.js applications, allowing developers to define custom CORS policies and manage cross-origin resource sharing effectively. By understanding and implementing CORS correctly, developers can enhance the security and usability of their Node.js applications....