billouboq / middie

Middlewares engine for Fastify.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

middie

js-standard-style Build Status

middie is the module that add middlewares support on steroids to Fastify.

The syntax style is the same as express/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.

Install

npm install middie --save

Usage

const Middie = require('middie')
const http = require('http')
const helmet = require('helmet')
const cors = require('cors')

const middie = Middie(_runMiddlewares)
middie.use(helmet())
middie.use(cors())

http
  .createServer(function handler (req, res) {
    middie.run(req, res)
  })
  .listen(3000)

function _runMiddlewares (err, req, res) {
  if (err) {
    console.log(err)
    res.end(err)
    return
  }

  // => routing function
}

Restrict middleware execution to a certain path(s)

If you need to run a middleware only under certains path(s), just pass the path as first parameter to use and you are done!
Note that this does not support routes with parameters, (eg: /user/:id/comments)

// Single path
middie.use('/public', staticFiles('/assets'))

// Multiple paths
middie.use(['/public', '/dist'], staticFiles('/assets'))

Acknowledgements

This project is kindly sponsored by:

License

Licensed under MIT.

About

Middlewares engine for Fastify.

License:MIT License


Languages

Language:JavaScript 100.0%