npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

access-control

v1.0.1

Published

Easily handle HTTP Access Control (CORS) in your applications

Downloads

55,656

Readme

HTTP Access-Control (CORS)

Version npmBuild StatusDependenciesCoverage StatusIRC channel

access-control implements HTTP Access Control, which more commonly known as CORS according to the W3 specification. The code is dead simple, easy to understand and therefor also easy to contribute to. access-control comes with a really simple API, so it's super simple, super awesome, super stable. All you expect from a small building block module as this.

Installation

npm install --save access-control

Usage

The module must first be configured before it can be used to add the correct CORS information to your HTTP requests. This is done by suppling the module with options.

'use strict';

var access = require('access-control');

After requiring the module you can supply the returned function with an options object which can contain the following properties:

var cors = access({
  maxAge: '1 hour',
  credentials: true,
  origins: 'http://example.com'
});

Now the cors variable contains a function that should receive your request and response. So it's as easy as:

var http = require('http').createServer(function (req, res) {
  if (cors(req, res)) return;

  res.end('hello world');
}).listen(8080);

You might have noticed that we've added an if statement around our cors function call. This is because the module will be answering the preflight request for you. So when it returns the boolean true you don't have to respond the request any more. In addition to the answering the option request is also answer the requests with a 403 Forbidden when the validation of the Access Control is failing.

In order to not waste to much bandwidth, the CORS headers will only be added if the request contains an Origin header, which should be sent by every request that requires HTTP Access Control information.

middleware

The library has build-in support for express based middleware (req, res, next). In fact, it's build in to the returned function so all you need to do is:

var app = express();

app.use(require('access-control')({ /* options here */ }));

And you have CORS handling enabled on your express instance. It's that easy.

Phonegap & Origin: null

If you're using Phonegap, your XHR requests will be sent with Origin: null as Origin header. In order to resolve this you must add the domain you are requesting to your origin white list:

http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html

This will ensure that the correct headers will be used for these cross domain/origin requests.

Related reading

If you're interested in learning more about HTTP Access Control (CORS) here's a good list to get started with:

License

MIT