DEV Community

Giuseppe Zileni
Giuseppe Zileni

Posted on

whitelistcors

CORS ( Cross-Origin Resource Sharing) is a mechanism that allows you to control the sources of requests on a Web page, which is particularly useful for defining security policies on your server, so that a browser and server interact to determine whether it is safe to allow cross-origin request.

Alt Text

The whitelistcors module helps developers configure CORS with a NodeJS and ExpressJS server and is very easy to use:

npm install --save whitelistcors
yarn add whitelistcors
Enter fullscreen mode Exit fullscreen mode

To enable a resource to a specific request from an HTTP source, you must implement only an array of addresses:

var whitelist = require('whitelistcors');

// lists of origin domains
var origins = ['http://localhost', 'http://mysite.com'];

app.post('/myapi', whitelist(origins), function(req, res, next) {
    res.send('CORS OK!');
})
Enter fullscreen mode Exit fullscreen mode

GitHub Projects: https://gzileni.github.io/whitelistcors/

Top comments (0)