Socket
Socket
Sign inDemoInstall

middie

Package Overview
Dependencies
4
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.1.0 to 5.0.0

.dependabot/config.yml

27

package.json
{
"name": "middie",
"version": "4.1.0",
"version": "5.0.0",
"description": "Middleware engine for Fastify",
"main": "middie.js",
"main": "index.js",
"scripts": {
"test": "standard && tap test.js",
"test": "standard && tap --no-coverage test/*.test.js",
"coverage": "tap --cov --coverage-report=html test.js"

@@ -33,16 +33,19 @@ },

"devDependencies": {
"cors": "^2.8.5",
"fastify": "^3.0.0-alpha.1",
"helmet": "^3.21.2",
"pre-commit": "^1.2.2",
"serve-static": "^1.12.4",
"standard": "^14.0.2",
"tap": "^12.6.5"
"serve-static": "^1.14.1",
"simple-get": "^3.1.0",
"standard": "^14.3.1",
"tap": "^14.10.5"
},
"dependencies": {
"path-to-regexp": "^4.0.0",
"reusify": "^1.0.2"
"fastify-plugin": "^2.0.0",
"path-to-regexp": "^6.1.0",
"reusify": "^1.0.4"
},
"greenkeeper": {
"ignore": [
"tap"
]
"engines": {
"node": ">=10.0.0"
}
}
# middie
[![Greenkeeper badge](https://badges.greenkeeper.io/fastify/middie.svg)](https://greenkeeper.io/)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ![](https://github.com/fastify/fastify/workflows/ci/badge.svg)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/fastify/middie.svg?branch=master)](https://travis-ci.org/fastify/middie)
*middie* is the plugin that add middlewares support on steroids to [Fastify](https://www.npmjs.com/package/fastify).
*middie* is the module that add middlewares support on steroids to [Fastify](https://www.npmjs.com/package/fastify).
The syntax style is the same as [express](http://npm.im/express)/[connect](https://www.npmjs.com/package/connect).
Does not support the full syntax `middleware(err, req, res, next)`, because error handling is done inside Fastify.
If you want to see how use this module with Fastify, check [here](https://github.com/fastify/fastify/#fastifyusemiddlewarereq-res-next).
## Install
```
npm install middie --save
npm install middie
```
<a name="usage"></a>
## Usage
Register the plugin and start using your middlewares.
```js
const Middie = require('middie')
const Fastify = require('fastify')
async function build () {
const fastify = Fastify()
await fastify.register(require('middie'))
// do you know we also have cors support?
// https://github.com/fastify/fastify-cors
fastify.use(require('cors')())
return fastify
}
build()
.then(fastify => fastify.listen(3000))
.catch(console.log)
```
### Encapsulation support
The encapsulation works as usual with Fastify, you can register the plugin in a subsystem and your code will work only inside there, or you can declare the middie plugin top level and register a middleware in a nested plugin, and the middleware will be executed only for the nested routes of the specific plugin.
*Register the plugin in its own subsystem:*
```js
const fastify = require('fastify')()
fastify.register(subsystem)
async function subsystem (fastify, opts) {
await fastify.register(require('middie'))
fastify.use(require('cors')())
}
```
*Register a middleware in a specific plugin:*
```js
const fastify = require('fastify')()
fastify
.register(require('middie'))
.register(subsystem)
async function subsystem (fastify, opts) {
fastify.use(require('cors')())
}
```
### Hooks and middlewares
Every registered middleware will be run during the `onRequest` hook phase, so the registration order is important.
Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Lifecycle/) documentation page to understand better how every request is executed.
```js
const fastify = require('fastify')()
fastify
.register(require('middie'))
.register(subsystem)
async function subsystem (fastify, opts) {
fastify.addHook('onRequest', async (req, reply) => {
console.log('first')
})
fastify.use((req, res, next) => {
console.log('second')
next()
})
fastify.addHook('onRequest', async (req, reply) => {
console.log('third')
})
}
```
### Restrict middleware execution to a certain path(s)
If you need to run a middleware only under certain path(s), just pass the path as first parameter to use and you are done!
```js
const fastify = require('fastify')()
const path = require('path')
const serveStatic = require('serve-static')
fastify
.register(require('middie'))
.register(subsystem)
async function subsystem (fastify, opts) {
// Single path
fastify.use('/css', serveStatic(path.join(__dirname, '/assets')))
// Wildcard path
fastify.use('/css/*', serveStatic(path.join(__dirname, '/assets')))
// Multiple paths
fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
}
```
# Middie Engine
You can also use the engine itself without the Fastify plugin system.
## Usage
```js
const Middie = require('middie/engine')
const http = require('http')

@@ -91,2 +192,12 @@ const helmet = require('helmet')

## Middlewares alternatives
Fastify offers some alternatives to the most commonly used middlewares, following, you can find a list.
| Express Middleware | Fastify Plugin |
| ------------- |---------------|
| [`helmet`](https://github.com/helmetjs/helmet) | [`fastify-helmet`](https://github.com/fastify/fastify-helmet) |
| [`cors`](https://github.com/expressjs/cors) | [`fastify-cors`](https://github.com/fastify/fastify-cors) |
| [`serve-static`](https://github.com/expressjs/serve-static) | [`fastify-static`](https://github.com/fastify/fastify-static) |
## Acknowledgements

@@ -96,7 +207,8 @@

- [nearForm](http://nearform.com)
Past sponsors:
- [LetzDoIt](http://www.letzdoitapp.com/)
## License
Licensed under [MIT](./LICENSE).
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc