Socket
Socket
Sign inDemoInstall

@fastify/middie

Package Overview
Dependencies
Maintainers
19
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/middie

Middleware engine for Fastify


Version published
Weekly downloads
264K
decreased by-23.09%
Maintainers
19
Weekly downloads
 
Created

What is @fastify/middie?

@fastify/middie is a Fastify plugin that allows you to use Express-style middleware in your Fastify application. This can be particularly useful for developers who are transitioning from Express to Fastify or who want to leverage existing middleware designed for Express.

What are @fastify/middie's main functionalities?

Using Express Middleware

This feature allows you to use Express-style middleware in your Fastify application. The code sample demonstrates how to register the @fastify/middie plugin and use a simple middleware function that logs incoming requests.

const fastify = require('fastify')();
const middie = require('@fastify/middie');

fastify.register(middie).then(() => {
  fastify.use((req, res, next) => {
    console.log('Request received');
    next();
  });

  fastify.get('/', (req, reply) => {
    reply.send({ hello: 'world' });
  });

  fastify.listen(3000, err => {
    if (err) throw err;
    console.log('Server listening on http://localhost:3000');
  });
});

Using Third-Party Middleware

This feature allows you to use third-party middleware designed for Express. The code sample demonstrates how to use the `cors` middleware to enable Cross-Origin Resource Sharing in your Fastify application.

const fastify = require('fastify')();
const middie = require('@fastify/middie');
const cors = require('cors');

fastify.register(middie).then(() => {
  fastify.use(cors());

  fastify.get('/', (req, reply) => {
    reply.send({ hello: 'world' });
  });

  fastify.listen(3000, err => {
    if (err) throw err;
    console.log('Server listening on http://localhost:3000');
  });
});

Chaining Multiple Middleware

This feature allows you to chain multiple middleware functions. The code sample demonstrates how to register multiple middleware functions that log messages to the console before handling a request.

const fastify = require('fastify')();
const middie = require('@fastify/middie');

fastify.register(middie).then(() => {
  fastify.use((req, res, next) => {
    console.log('First middleware');
    next();
  });

  fastify.use((req, res, next) => {
    console.log('Second middleware');
    next();
  });

  fastify.get('/', (req, reply) => {
    reply.send({ hello: 'world' });
  });

  fastify.listen(3000, err => {
    if (err) throw err;
    console.log('Server listening on http://localhost:3000');
  });
});

Other packages similar to @fastify/middie

Keywords

FAQs

Package last updated on 14 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc