Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hugojosefson/middleware-only-at-path
Only use Express middleware at specific path.
https://github.com/hugojosefson/middleware-only-at-path
Last synced: 29 days ago
JSON representation
Only use Express middleware at specific path.
- Host: GitHub
- URL: https://github.com/hugojosefson/middleware-only-at-path
- Owner: hugojosefson
- Created: 2016-02-18T16:57:54.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-12T04:07:42.000Z (4 months ago)
- Last Synced: 2024-11-15T20:05:45.603Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# middleware-only-at-path
Lets you use an Express middleware, only at a specific path.
Useful as `app.use(onlyAt('/', middleware))` so it doesn't run the middleware any deeper than
exactly `/`.This means you don't have to write the routes in a backwards order for the middlewares to take
effect only where you want them.## Install
```bash
npm install --save middleware-only-at-path
```## Example
If you want to use [allow-methods](https://www.npmjs.com/package/allow-methods) at specific
paths, you can do this:```js
import express from 'express';
import onlyAt from 'middleware-only-at-path';
import allowMethods from 'allow-methods';const app = express();
app.use(onlyAt('/', allowMethods(['OPTIONS', 'GET', 'HEAD'])));
app.get('/', (req, res) => res.send('Hello world!'));app.use(onlyAt('/examples', allowMethods(['OPTIONS', 'POST'])));
app.post('/examples', (req, res) => res.status(201).send('Created.'));app.listen(8000);
```