This is a simple Node.js server application that allows users to upload images and videos to the server. The application uses Express and the Express File Uploader middleware to handle file uploads. The uploaded files are then stored on Cloudinary, a cloud-based image and video management service. After a successful upload, the application sends an email to the user who uploaded the file, containing a link to the uploaded file.
To get started with this application, follow these steps:
- Clone the repository to your local machine.
- Install the required dependencies using
npm install. - Set up the environment variables for Cloudinary and your email service. You can do this by creating a
.envfile in the root directory of the project and adding the following variables:
CLOUDINARY_CLOUD_NAME=<your cloudinary cloud name>
CLOUDINARY_API_KEY=<your cloudinary api key>
CLOUDINARY_API_SECRET=<your cloudinary api secret>
EMAIL_SERVICE_USER=<your email service user>
EMAIL_SERVICE_PASSWORD=<your email service password>- Start the server using
npm start.
The application consists of four main files:
index.js: This file sets up the Express app, connects to the database and Cloudinary, and defines the endpoints for file upload.routes/FileUpload.js: This file defines the routes for file upload, which includeimageUpload,videoUpload, andimageSizeReducer.models/File.js: This file defines the schema for the uploaded files and defines a post-save hook that sends an email to the user who uploaded the file.controllers/fileUpload.js: This file contains the logic for handling file uploads, including checking file types, uploading to Cloudinary, and saving the file to the database.
The application provides the following endpoints for file upload:
POST /api/image-upload: This endpoint handles image uploads. It accepts afileparameter containing the image to be uploaded.POST /api/video-upload: This endpoint handles video uploads. It accepts afileparameter containing the video to be uploaded.POST /api/image-size-reducer: This endpoint handles image size reduction. It accepts afileparameter containing the image to be resized and awidthparameter specifying the desired width of the output image.
This application uses the following dependencies:
express: A web framework for Node.js.express-fileupload: A middleware for handling file uploads in Express.cloudinary: A Node.js library for the Cloudinary API.mongoose: A MongoDB object modeling tool for Node.js.nodemailer: A module for sending emails from Node.js.dotenv: A module for loading environment variables from a.envfile.