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

gridfs-express

v2.1.0

Published

node package to mount a gridfs API on an express router

Downloads

8

Readme

gridfs-express

build status

Provide an API REST which support upload/download files to/from a modgodb grid. This package enable the user to define the API on a given Express router.

A complete example of a service using this package can be found at example-gridfs-express

Installation

npm install gridfs-express

Usage

const express = require('express');
const mongodb = require('mongodb');
const bodyParser = require('body-parser');
const expressListRoutes = require('express-list-routes');
const gridfs = require('gridfs-express');

const app = express();

const url = "mongodb://localhost:27017/gridfs_test";

let db;

const routerAPI = express.Router();

gridfs(routerAPI, {
  getDb: () => db,
  getKeyMetadata: (req) => ({id: req.query.id}),
  getOtherMetadata: (req) => req.body,
  fsCollections: [
    'input',
    'output'
  ]
});

app.use(bodyParser.json());
app.use('/api/gridfs', routerAPI);
expressListRoutes({prefix: '/api/gridfs'}, 'API:', routerAPI);

mongodb.MongoClient.connect(url, function(err, database) {
  if(err) throw err;

  db = database;
  // Start the application after the database connection is ready
  app.listen(3000);
});

The function gridfs define the following API on the given router object.

| route | verb | URL parameters | description | | ------------------------ | ------ | ------------------------------------ | ------------------------------------------ | | /api/gridfs | GET | fs=[string] | get all files info | | /api/gridfs/:id | GET | fs=[string]key=['filename','id'] | get a single file | | /api/gridfs/:id/info | GET | fs=[string]key=['filename','id'] | get a sigle file, include a metadata field | | /api/gridfs | POST | fs=[string] | upload a new file | | /api/gridfs/:id | DELETE | fs=[string]key=['filename','id'] | delete a single file | | /api/gridfs/:id/metadata | PATCH | fs=[string]key=['filename','id'] | modify the metadata field |

The parameter :id is considered either as an identifier or filename depending on the value of query parameter key which could take on of the values id or filename respectively.

Note that the path /api/gridfs depends on the user election.

Options

The second argument to the function gridfs is an object with options to configure the endpoints. It is required to provide a function member to get the mongodb connection:

{
  getDb: () => {return db}
}

Other members which are optional are:

  • maxFileSize: the maximum file size allowed. The default limit is 200MB.
  • fsCollections: it is an array of string which contains the allowed collection names to be used in the query parameter fs. This array is used to validate the query parameter fs.
  • getKeyMetadata: it is a function which should return an object. This object will be associated to the file in the gridfs collection. The filename will be unique for that object value. This objects is stored within the metadata for the file in the gridfs. The function will receive as argument the request object req. For instance, the following function build the key object from the url query parameter id:
function getKeyMetadata(req) {
  return {id: req.query.id};
}
  • getMetadata: it is a function which should return an object, this object will be stored within the metadata for the file. The function will receive as argument the request object req. The returned object should not contain the keys returned from getKeyMetadata, they will be discarded. For instance, the following function build the an extra metadata object from the query parameters tag1 and tag2:
function getOtherMetadata(req) {
  return {
    tag1: req.query.tag1,
    tag2: req.query.tag1
  };
}

Tests

npm test

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.