🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

cors-except

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cors-except

An exception middleware designed for use with CORS, but will accept any middleware.

1.1.0
latest
Source
npm
Version published
Weekly downloads
11
450%
Maintainers
1
Weekly downloads
 
Created
Source

NPM license GitHub contributors GitHub issues GitHub release (latest by date) GitHub release date NPM downloads GitHub package.json dynamic

cors-except

This package provides a middleware that allows CORS requests to be selectively checked. It is useful for services that needs to be accessed by browsers.

Usage

import cors from "cors"
import express from "express"
import except from "cors-except"

const server = express()

const whitelist = ["https://frontend.url"]
const corsOptions = {
  origin: (origin, next) => {
    try {
      if (whitelist.indexOf(origin) !== -1) next(null, true)
      else next(new Error("Not allowed by CORS"))
    } catch (error) {
      next(error)
    }
  }
}

const corsExceptions = ["/without-cors"]
server.use(except(corsExceptions, cors(corsOptions)))

server.get("/without-cors", (_, res) => res.send("Any origin"))
server.get("*", (_, res) => res.send("corsOptions origin only"))
server.listen(() => console.log("Server started"))

Keywords

cors

FAQs

Package last updated on 11 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts