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

cors-helper

v1.0.8

Published

Simple pattern matching helper for allowing/blocking certain domains to support dynamic CORS validation in a NodeJS application

Downloads

20

Readme

Cors Helper

This piece of ExpressJs middleware exists to make it easier to configure CORS settings in a NodeJs app with either a list of blocked or allowed IPs/URLs/Domains.

Installation

npm install cors-helper

Usage

A somewhat standard Express app would setup some key pieces of middleware (body-parser, compression, helmet, etc), but rather than using cors middleware out-of-the-box (by default cors() will allow everything!), instead you can pass it a function to dynamically handle origins. This cors-helper package provides you two factory functions that will create that function for you (depending on whether you want to block or allow certain IPs/URLs/Domains).

const cors = require('cors')
const helmet = require('helmet')
const express = require('express')
const bodyParser = require('body-parser')
const compression = require('compression')
const { createBlockedListMiddleware } = require('cors-helper')

// An array of strings representing all the sites you DON'T want to allow
const listOfBadSites = require('./block_these_ips_and_domains.json')

const corsOptions = createBlockedListMiddleware(listOfBadSites)

const port = process.env.PORT || 5000

express()
  .use(bodyParser.json({ limit: '4mb' }))
  .use(bodyParser.urlencoded({ extended: true }))
  .use(compression)
  .use(cors(corsOptions))
  .use(helmet)
  .listen(port, () => console.log(`app is now listening on port ${port}`))

API

This package exports two named factory functions that take a list of IPs, URLs, and/or Domains that you will be able to allow or block when your cors middleware is set up.

  • allowCrossDomainMiddleware - A common piece of middleware that is used (mostly) for local development to completely turn off cors validation by setting certain headers (not typically used in production)
  • createBlockedListMiddleware - A function that takes an Array of (String) values that represent the IPs, URLs, and/or Domains to reject (everything else will be allowed)
  • createAllowedListMiddleware - A function that takes an Array of (String) values that represent the IPs, URLs, and/or Domains to allow (everything else will be rejected)