https://github.com/mrsteele/next-middleware
Create middleware for your next route
https://github.com/mrsteele/next-middleware
Last synced: 3 months ago
JSON representation
Create middleware for your next route
- Host: GitHub
- URL: https://github.com/mrsteele/next-middleware
- Owner: mrsteele
- License: mit
- Created: 2017-12-11T01:12:18.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-14T22:26:27.000Z (over 7 years ago)
- Last Synced: 2024-12-21T09:39:33.600Z (5 months ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# next-middleware
Create middleware for your next route### Install
You can install using npm
```
npm i next-middleware --save
```### Usage
The first step is to create your **middleware.js** file:
```js
// import this module
import Middleware from 'next-middleware'// Create a new middleware pipeline
const middleware = new Middleware()// we can go ahead and export the build function as the default
export default middleware.build// Add whatever middleware you want
middleware.add(async ({ req }) => {
const users = fetch(`/users?id=${req.query.id}`)
return { users }
})```
Then you can use it anywhere in `/pages/*.js`
```js
import middleware from '../middleware'const Page = ({ users }) => (
Users: {users.map(user => user.name).join()}
)// wrap it with your middleware
export default middleware(Page)
```### License
MIT