npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-filepass

v1.0.1

Published

A middleware for handling file uploads with validation in Express.js

Downloads

13

Readme

express-filepass

A middleware for handling file uploads with validation in Express.js.

Installation

To install the package, use npm or yarn:

npm install express-filepass
# or
yarn add express-filepass

Usage

Basic Setup

Here's how to use the express-filepass middleware in your Express application.

const express = require('express');
const { body, validationResult } = require('express-validator');
const uploader = require('express-filepass');
const User = require('./models/User'); // Assuming you have a User model

const app = express();

// Define validation rules
const validateForm = [
  body('username').notEmpty().withMessage('Name is required').isString().withMessage('Invalid value'),
  body('email').isEmail().withMessage('Valid email is required'),
  body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters long')
];

// Define file upload options
const uploadOptions = {
  avatar: {
    allowedTypes: ['image/jpg', 'image/jpeg', 'image/png'],
    maxSize: 5 * 1024 * 1024, // 5 MB
    uploadDir: './public/uploads/avatar',
    multiple: false,
    maxFiles: 1,
    errorMessage: {
      invalidType: 'Only .jpg, .png, .jpeg type allowed',
      sizeExceeded: 'File size exceeds the limit of 5MB',
      maxFiles: 'One file is allowed to be uploaded'
    }
  }
};

// Middleware to handle form validation and file uploads
app.post('/upload', uploader(validateForm, uploadOptions), async (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }

  try {
    let file = req.body.avatar ? req.body.avatar[1] : null;
    const newUser = new User({
      username: req.body.username,
      email: req.body.email,
      password: req.body.password,
      avatar: file
    });

    await newUser.save();
    res.status(200).json(newUser);
  } catch (error) {
    console.error(error.message);
    res.status(500).json({ msg: 'Internal Server Error' });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

Options

File Upload Options

The uploadOptions object allows you to specify various settings for handling file uploads. Here's a detailed description of each option:

| Option | Type |Description | | ------ | ------ |------ | | allowedTypes | Array |An array of allowed MIME types for the files. | | maxSize | Number | The maximum file size allowed (in bytes)| | uploadDir | String | The directory where the uploaded files will be saved| | multiple | Boolean | Whether multiple files can be uploaded for the given field| | maxFiles | Number | The maximum number of files allowed to be uploaded for the given field| | invalidType | String | Error message when an invalid file type is uploaded| | sizeExceeded | String | Error message when the uploaded file exceeds the allowed size| | maxFiles | String | Error message when the number of uploaded files exceeds the allowed limit|

Example Configuration

const uploadOptions = {
  avatar: {
    allowedTypes: ['image/jpeg', 'image/png', 'image/jpg'],
    maxSize: 5 * 1024 * 1024, // 5 MB
    uploadDir: './public/uploads/avatar',
    multiple: false,
    maxFiles: 1,
    errorMessage: {
      invalidType: 'Only .jpeg, .png, .jpg type allowed',
      sizeExceeded: 'File size exceeds the limit of 5MB',
      maxFiles: 'One file is allowed to be uploaded'
    }
  },
  documents: {
    allowedTypes: ['application/pdf'],
    maxSize: 5 * 1024 * 1024, // 5 MB
    uploadDir: './public/uploads/documents',
    multiple: true,
    maxFiles: 5,
    //if you do not give this error message, the default error message will occur.
    errorMessage: {
      invalidType: 'Only .pdf type allowed',
      sizeExceeded: 'File size exceeds the limit of 5MB',
      maxFiles: 'Five file is allowed to be uploaded'
    }
  }
};

Form Validation

The package integrates seamlessly with express-validator for form validation. You define your validation rules as shown below:

const validateForm = [
  body('username').notEmpty().withMessage('Name is required').isString().withMessage('Invalid value'),
  body('email').isEmail().withMessage('Valid email is required'),
  body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters long')
];

Full Example

Here's a full example demonstrating the use of express-filepass with both file upload and form validation:

const express = require('express');
const { body, validationResult } = require('express-validator');
const uploader = require('express-filepass');
const User = require('./models/User'); // Assuming you have a User model

const app = express();

const validateForm = [
  body('username').notEmpty().withMessage('Name is required').isString().withMessage('Invalid value'),
  body('email').isEmail().withMessage('Valid email is required'),
  body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters long')
];

const uploadOptions = {
  avatar: {
    allowedTypes: ['image/jpeg', 'image/png', 'image/jpg'],
    maxSize: 5 * 1024 * 1024, // 5 MB
    uploadDir: './public/uploads/avatar',
    multiple: false,
    maxFiles: 1,
     errorMessage: {
      invalidType: 'Only .jpeg .png .jpg type allowed',
      sizeExceeded: 'File size exceeds the limit of 5MB',
      maxFiles: 'One file is allowed to be uploaded'
    }
  }
};

app.post('/upload', uploader(validateForm, uploadOptions), async (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }

  try {
    let file = req.body.avatar ? req.body.avatar[1] : null;
    const newUser = new User({
      username: req.body.username,
      email: req.body.email,
      password: req.body.password,
      avatar: file
    });

    await newUser.save();
    res.status(200).json(newUser);
  } catch (error) {
    console.error(error.message);
    res.status(500).json({ msg: 'Internal Server Error' });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

Contributing

If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on GitHub.

Explanation

  • Installation: Instructions on how to install the package using npm or yarn.
  • Usage: A basic example showing how to set up and use the middleware in an Express application.
  • Options: Detailed table explaining the different options available for configuring file uploads.
  • Example Configuration: A configuration example for multiple file fields with different settings.
  • Form Validation: Instructions on how to validate form data using express-validator.
  • Full Example: A complete example demonstrating the usage of the package with both file uploads and form validation.
  • Contributing: Information on how to contribute to the project.
  • License: Licensing information for the project.

By following this README.md file, users will have a clear understanding of how to install, configure, and use your express-filepass package.

License

MIT

Free Software, Hell Yeah!