1.1.0 • Published 3 years ago

cors-except v1.1.0

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
3 years ago

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"))
1.0.2

3 years ago

0.1.0

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

0.0.2

3 years ago

1.0.3

3 years ago

0.0.1

3 years ago

1.0.0

3 years ago