cors
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
object-assign | 4.1.1 | 2.61 kB | MIT | prod | |
vary | 1.1.2 | 3.68 kB | MIT | prod |
The CORS package in Node.js is used as middleware for Connect/Express to enable Cross-Origin Resource Sharing (CORS). With the help of CORS, resources from various origins are made available to the browsers. Various options can be implemented for CORS as per requirements.
To use the CORS package, you can begin by installing it using the npm install command npm install cors
. You can then import it into your code with var cors = require('cors')
. You can enable CORS for all requests by adding app.use(cors())
or for a specific route, using app.get('/your-route', cors(), yourHandlerFunction)
. Further customizations and configurations pertaining to CORS such as setting origins, handling preflight requests, and configuring dynamic CORS are also possible. Here is an example of enabling CORS for all requests:
var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'});
});
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80');
});
The documentation for CORS can be found in the repository available at https://github.com/expressjs/cors
. The readme file provides details about installation, usage, and configuration options. For a deep dive into the effects of various CORS headers, the article at http://www.html5rocks.com/en/tutorials/cors/
can be referred to.