Skip to content

Commit

Permalink
docs: add middlewares section
Browse files Browse the repository at this point in the history
  • Loading branch information
tadaii committed Sep 28, 2022
1 parent a1c836c commit ff5906e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
1 change: 0 additions & 1 deletion docs/_media/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
--cover-background-color: rgb(0,22,79);
--cover-background-image: linear-gradient(7deg, transparent, rgba(3,35,124, 0.9));
--cover-color: rgb(209,237,255);
--table-row-even-background: var(--mono-shade1);
}

/* Cover page customization */
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Static mocks](static-mocks.md)
- [Dynamic mocks (services)](dynamic-mocks.md)
- [Db API](db-api.md)
- [Middlewares](middlewares.md)
- [Configuration](configuration.md)
- [Scraping](scraping.md)
- [CLI commands](commands.md)
Expand Down
26 changes: 0 additions & 26 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,3 @@ module.exports = {
| `extendServer` | - | Used to set custom instructions to the server application. Must be a function with the following signature: function ({ server, app, db }) {}. `server` being the node http.Server instance, `app` the [h3](https://github.com/unjs/h3) instance and `db` the [drosse db api](db-api#api). |
| `onHttpUpgrade` | `null` | A function that initiates a websocket connection. This is happening once during HTTP protocol upgrade handshake. Must be a function with the following signature: function (request, socket, head) { ... }. |
| `commands` | - | Used to extend Drosse CLI with custom commands. Must be a function with the following signature: function (vorpal, drosse) { ... }. See the [CLI commands](commands.md) documentation. |

## Custom middlewares

You can create your own middlewares. Simply create a JS file that exports a classic express middleware function. Something like this:

```js
module.exports = function (req, res, next) {
// put your middleware code here
next()
}
```

You can also define the middleware with a supplementary argument, to place at the first position. It will
expose the Drosse API inside your middleware, letting you access the `db` instance for example.

```js
module.exports = function (api, req, res, next) {
// (very) naive role checking :)
const { db } = api
const user = db.get.byId('users', req.params.id)
if (user.role !== 'admin') {
return next({ some: 'error'})
}
next()
}
```
25 changes: 25 additions & 0 deletions docs/middlewares.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Middlewraes

You can create your own middlewares. Simply create a JS file that exports a classic express middleware function. Something like this:

```js
module.exports = function (req, res, next) {
// put your middleware code here
next()
}
```

You can also define the middleware with a supplementary argument, to place at the first position. It will
expose the Drosse API inside your middleware, letting you access the `db` instance for example.

```js
module.exports = function (api, req, res, next) {
// (very) naive role checking :)
const { db } = api
const user = db.get.byId('users', req.params.id)
if (user.role !== 'admin') {
return next({ some: 'error'})
}
next()
}
```

0 comments on commit ff5906e

Please sign in to comment.