Socket
Socket
Sign inDemoInstall

@fastify/multipart

Package Overview
Dependencies
Maintainers
0
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/multipart

Multipart plugin for Fastify


Version published
Weekly downloads
186K
decreased by-0.86%
Maintainers
0
Weekly downloads
 
Created

What is @fastify/multipart?

@fastify/multipart is a Fastify plugin for handling multipart/form-data, which is commonly used for file uploads. It provides a simple and efficient way to handle file uploads in Fastify applications.

What are @fastify/multipart's main functionalities?

Basic File Upload

This code demonstrates a basic file upload endpoint using @fastify/multipart. The file is processed and its filename is returned in the response.

const fastify = require('fastify')();
const fastifyMultipart = require('@fastify/multipart');

fastify.register(fastifyMultipart);

fastify.post('/upload', async (req, reply) => {
  const data = await req.file();
  await data.toBuffer(); // Process the file
  reply.send({ filename: data.filename });
});

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

Handling Multiple Files

This code demonstrates handling multiple file uploads in a single request using @fastify/multipart. Each file is processed in a loop.

const fastify = require('fastify')();
const fastifyMultipart = require('@fastify/multipart');

fastify.register(fastifyMultipart);

fastify.post('/upload-multiple', async (req, reply) => {
  const parts = req.files();
  for await (const part of parts) {
    await part.toBuffer(); // Process each file
  }
  reply.send({ status: 'Files processed' });
});

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

File Validation

This code demonstrates file validation using @fastify/multipart. It restricts uploads to files of type 'image/png' and limits the file size to 1MB.

const fastify = require('fastify')();
const fastifyMultipart = require('@fastify/multipart');

fastify.register(fastifyMultipart, {
  limits: {
    fileSize: 1000000 // 1MB
  }
});

fastify.post('/upload', async (req, reply) => {
  const data = await req.file();
  if (data.mimetype !== 'image/png') {
    reply.code(400).send({ error: 'Only PNG files are allowed' });
    return;
  }
  await data.toBuffer(); // Process the file
  reply.send({ filename: data.filename });
});

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

Other packages similar to @fastify/multipart

Keywords

FAQs

Package last updated on 22 Sep 2024

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