
Build a Node.js File Upload API with Express and Multer
Want to add file upload functionality to your Node.js application? This guide provides a step-by-step approach using Express, TypeScript, and Multer. Learn to build a clean, reusable backend API for handling file uploads easily.
Why Use Multer for File Uploads in Node.js?
Multer is middleware for Node.js that simplifies handling multipart/form-data
, which is essential for file uploads. It streamlines the process of receiving and processing files on your server.
Tech Stack for Our File Upload API
- Express: A fast, minimalist web framework for Node.js.
- Multer: Middleware for handling
multipart/form-data
. - TypeScript: Adds optional static typing to your JavaScript code, improving maintainability.
- MVC Structure: Organizes your code for clarity and scalability.
Project Structure: Keep it Clean
Organize your project with a clear folder structure:
file-upload-api/
├── controllers/
│ └── upload.controller.ts
├── middleware/
│ └── upload.middleware.ts
├── routes/
│ └── upload.route.ts
├── services/
│ └── upload.service.ts
├── uploads/ (created automatically)
├── index.ts
├── tsconfig.json
└── package.json
Step-by-Step Guide to Building a Node.js File Upload API
Let's create a simple file upload API.
Step 1: Installing the Necessary Dependencies
First, install the packages you'll be using:
Create a tsconfig.json
if you don't already have one:
Step 2: Configure Multer Middleware
Create middleware/upload.middleware.ts
to handle file storage:
This middleware configures Multer to save uploaded files to the uploads
directory, prefixing the original filename with a timestamp to prevent collisions.
Step 3: Implementing the Upload Service
Create services/upload.service.ts
to process the uploaded file details:
This service formats the file information into a user-friendly object.
Step 4: Create the File Upload Controller
Create controllers/upload.controller.ts
to handle the upload request:
This controller checks for a file, processes it using the upload service, and sends a response.
Step 5: Define the API Route for Node.js File Upload
Create routes/upload.route.ts
to define the API endpoint:
Here, we define a route /file
that uses the Multer middleware to handle a single file upload named "file"
.
Step 6: Setting Up the Express Server
Create index.ts
to initialize the Express server and connect the route:
This code starts an Express server that listens on port 3000 and uses the upload route you've created.
Testing Your File Upload API
Use Postman or a similar tool to test your API which simplifies testing file uploads in Node.js.
- Method: POST
- URL:
http://localhost:3000/api/upload/file
- Body:
form-data
- Key:
file
(Type: File) - Select a file from your system
- Key:
If successful, you'll receive a JSON response containing file details, similar to:
Conclusion
You now have a functional and well-structured Node.js file upload API using Express, TypeScript, and Multer. This setup provides a solid foundation for building more complex file handling features in your applications. Embrace this stack for scalable and maintainable solutions.