gridfs-express
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 = ;const mongodb = ;const bodyParser = ;const expressListRoutes = ;const gridfs = ; const app = ; const url = "mongodb://localhost:27017/gridfs_test"; let db; const routerAPI = express; ; app;app;; mongodbMongoClient;
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:
{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 parameterfs
. This array is used to validate the query parameterfs
.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 objectreq
. For instance, the following function build the key object from the url query parameter id:
{ return id: reqqueryid;}
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 objectreq
. 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:
{ return tag1: reqquerytag1 tag2: reqquerytag1 ;}
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.