cors-except
v1.1.0
Published
An exception middleware designed for use with CORS, but will accept any middleware.
Downloads
8
Maintainers
Readme
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"))