Giter Club home page Giter Club logo

file-server's Introduction

Uploading Files to MongoDB with GridFS and Multer Using NodeJS, Nginx (web server) and mongo-express(Web-based MongoDB admin interface, written with Node.js and express)

GridFS is a specification that describes how to split files into chunks during storage and reassemble them during retrieval. The driver implementation of GridFS manages the operations and organization of the file storage.

You should use GridFS if the size of your file exceeds the BSON-document size limit of 16 megabytes.

GridFS organizes files in a bucket, a group of MongoDB collections that contain the chunks of files and descriptive information. Buckets contain the following collections, named using the convention defined in the GridFS specification:

  • The chunks collection stores the binary file chunks.
  • The files collection stores the file metadata.

When you create a new GridFS bucket, the driver creates the chunks and files collections, prefixed with the default bucket name fs, unless you specify a different name.

GridFS-upload

​ Image source: mongodb.com

Using this packages:

  • Mongoose (This package will translate the node.JS code to MongoDB)
  • Config (It lets you define a set of default parameters, and extend them for different deployment environments.
  • Express (You’ll need this package for any HTTP requests you want to run)
  • BodyParser (This package lets you receive content from HTML forms)
  • Multer (This package enables easy file upload into MongoDB
  • Gridfs-stream (Easily stream files to and from MongoDB GridFS.)
  • Multer-gridfs-storage (You need this package to implement the MongoDB GridFS feature with multer).

docker-compose.yml:

version: "3"
services:
  backend-file-server:
    image: file-server
    container_name: file-server
    build:
      context: .
    restart: on-failure
    depends_on: 
      - mongodb
    networks:
      - file-net
  mongodb:
    image: mongo:4.2
    container_name: mongodb
    restart: on-failure
    env_file: ./mongo_env
    volumes: 
      - ./mongo-data:/data/db
    networks:
      - file-net
  mongo-express:
    image: mongo-express:0.54.0
    container_name: mongo-express
    depends_on:
      - mongodb
    networks:
      - file-net
    env_file: ./mongo-express_env 
  nginx:
    image: nginx:1.21
    container_name: proxy_server
    restart: on-failure
    depends_on:
      - backend
    networks:
      - file-net
    ports:
      - "8080:8080"
      - "8081:8081"
    volumes:
      - ./conf.d/:/etc/nginx/conf.d/
networks:
  file-net:

Dockerfile:

FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "start"]

Run application:

Note: This instruction uses 'docker compose' version 2.

# Start Services
docker compose up -d --build
# Stop Services
docker compose down
# All Services Logs
docker compose logs -f
# Log a Specific Service
docker compose logs -f <Service_Name>

file-server's People

Contributors

azita-abdollahi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.