Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zignis/deflight
Bypass express middlewares for pre-flight requests
https://github.com/zignis/deflight
express middleware preflight
Last synced: 10 days ago
JSON representation
Bypass express middlewares for pre-flight requests
- Host: GitHub
- URL: https://github.com/zignis/deflight
- Owner: zignis
- License: mit
- Created: 2023-02-24T16:00:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-14T13:33:19.000Z (about 2 months ago)
- Last Synced: 2024-10-12T17:33:48.914Z (26 days ago)
- Topics: express, middleware, preflight
- Language: TypeScript
- Homepage: https://npmjs.com/package/deflight
- Size: 167 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# [De-flight](https://npmjs.com/package/deflight)
A middleware wrapper for [express](https://expressjs.com/) that skips calling the middleware for pre-flight requests.
## Installation
```shell
# Yarn
yarn add deflight# NPM
npm install deflight
```The package exports both a named and a default export:
```js
import { deflight } from "deflight";
// Or
import deflight from "deflight";app.use(deflight(someMiddleware));
```## When to use it?
If your app serves requests coming from a different origin than your server is hosted on, and you need to do something specifically with the pre-flight requests, for example, sending the `Access-Control-Allow-Methods` header on a per-route basis:
```js
app.use(deflight(someExpensiveMiddleware));app.all('/example', (req, res, next) => {
if ((req.method || '').toLowerCase() === 'options') {
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Content-Length', '0');return res.status(204).end();
}
// Route logic
});
```## Gotchas
### Using with the [CORS](https://expressjs.com/en/resources/middleware/cors.html) middleware
You need to enable the `preflightContinue` option to let the CORS middleware pass the pre-flight request
to subsequent middlewares and not return early.```js
app.use(
cors({
origin: "https://example.com",
preflightContinue: true, // Required
})
);
```### TypeScript
The wrapper uses the default `Request` type from the express package.
If you have extended the request object, or your middleware expects a different request object:```ts
interface ExtendedRequest extends Request {
customProp: string;
}app.use(deflight(someMiddleware));
```## License
Deflight is released under the [MIT License](./LICENSE).