How to Upload Download Files in Node.js Using Express, Multer and MongoDB
In this tutorial, we will walk through how to build a complete file upload pipeline from scratch using Node.js, Express, Multer (for handling multipart form data), EJS (for server-side rendering), and Mongoose (to store file paths and metadata in MongoDB).
Prerequisites & Tech Stack
Make sure you have Node.js and MongoDB installed on your system. We will use the following packages:
Express: Fast web framework for handling server routes and requests.
EJS: Template engine used to render dynamic HTML views.
Body-Parser: Middleware to parse incoming form request bodies.
Multer: Middleware specifically designed for handling
multipart/form-datafile uploads.Mongoose: Object Data Modeling (ODM) library to connect and interact with MongoDB.
Step 1: Setup Your Project Directory
Open your console or terminal and create a new folder for your project, then navigate inside it using these commands:Step 2: Initalize Package.Json
Initialize your Node.js environment automatically by running the following command in your terminal:package.json file, which tracks your project's metadata and installed dependencies.Step 3: Install Required Dependencies
node_modules folder will be generated, and your package.json dependencies list will automatically update.Step 4: Configure Project Structure and Entry Point
Create a new file in your project root directory named
app.js.Open your
package.jsonfile and make sure your scripts object includes a start command:"start": "node app.js".Create the required folder structure so your server knows where to store files and view templates:
views/(Contains your frontend template files, such ashome.ejs)public/uploads/(Static folder where uploaded files will be physically saved on your server disk)
Step 5:Write the Backend Logic(app.js)
Open your
app.jsfile and add the following code. This file ties together your Express server, MongoDB connection via Mongoose, and Multer file storage configuration.
How the Backend Code Works:
mongoose.connect(): Establishes a connection to your local MongoDB instance (mongodb://localhost:27017/picspics). The database will be created automatically on your server when data is first inserted.app.set("views", ...): Tells Express where to locate your template views folder.app.set("view engine", "ejs"): Instructs Express to render files ending in.ejsusing the EJS engine.body-parser: Parses incoming request bodies so you can easily access form input fields.- Mongoose Schema & Model: Defines the structure (fields) of your collection (
mongoose.Schema({})) and registers the model (mongoose.model()). upload.single('pic'): This is the core Multer middleware. The string'pic'must match thenameattribute of your file input tag in your HTML form (<input type="file" name="pic">).diskStorage: Tells Multer to save uploaded files into the./public/uploadsdirectory while keeping their original filenames.res.download(): Express built-in helper method that triggers a file download prompt in the user's browser for the specified file path.
Step 6: Create the Frontend View Template (views/home.ejs)
home.ejs inside your views/ directory. This file provides a clean user interface with a form to upload files and a gallery grid to display previously uploaded files with download buttons.Step 7: Run and Test Your Application
- Start your application from your terminal:
- Open your web browser and go to:
- Select an image file, click Upload File, and watch it instantly save to your server disk and appear in your gallery!
Comments
Post a Comment