@tinyhttp/cors
tinyhttp CORS module
Installation
npm install @tinyhttp/cors
API
import { cors } from '@tinyhttp/cors'
Options
host
Host that is allowed to send cross-origin requests. Defaults to '*'.
methods
Allowed methods for performing a cross-origin request. Default ones are ['GET', 'POST', 'PUT', 'PATCH', 'HEAD']
and can be accessed with defaultMethods
:
import { App } from '@tinyhttp/app'
import { defaultMethods, cors } from '@tinyhttp/cors'
const app = new App()
app.use(
cors({
methods: defaultMethods.filter(m => m !== 'DELETE'),
host: 'https://example.com'
})
)
Allowed HTTP headers that can be sent in a cross-origin request. Default ones are ['Origin', 'X-Requested-With', 'Content-Type']
and can be accessed with defaultHeaders
:
import { App } from '@tinyhttp/app'
import { defaultHeaders, cors } from '@tinyhttp/cors'
const app = new App()
app.use(
cors({
methods: [...defaultHeaders, 'X-Custom-Header'],
host: 'https://example.com'
})
)
Example
import { App } from '@tinyhttp/app'
import { cors } from '@tinyhttp/cors'
const app = new App()
app
.use(
cors({
host: 'https://example.com'
})
)
.get('/', (_, res) => void res.end('Hello World'))
License
MIT