---
title: "Create an Express.js route to upload files to the server using Multer middleware."  
description: "Create an Express.js route to upload files to the server using Multer middleware."  
author: "Steilla Mitchel"  
published: 2023-07-26  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/159300/create-an-express-js-route-to-upload-files-to-the-server-using-multer-middleware  
category: "javascript"  
tags: ["javascript", "express js"]  
reading_time: 2 minutes  

---

# Create an Express.js route to upload files to the server using Multer middleware.

Create an Express.js [route](https://www.mindstick.com/interview/396/what-is-routed-event-in-wpf) to [upload](https://www.mindstick.com/forum/12860/how-to-upload-file-asynchronously-using-generic-handler) [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) to the [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) using Multer middleware.

## Replies

### Reply by Aryan Kumar

Sure, here is the code for an Express JS route to upload files to the server using Multer middleware:

JavaScript

```plaintext
const express = require("express");
const multer = require("multer");

const app = express();

const upload = multer({
  storage: multer.diskStorage({
    destination: "./uploads",
    filename: (req, file, cb) => {
      const name = file.originalname.split(".")[0];
      const ext = file.originalname.split(".")[1];
      const filename = `${name}-${Date.now()}.${ext}`;
      cb(null, filename);
    },
  }),
});

app.post("/upload", upload.single("file"), (req, res) => {
  const file = req.file;
  console.log(file);
  res.send("File uploaded successfully!");
});

app.listen(3000, () => {
  console.log("Server started on port 3000");
});
```

This code first imports the `express` and `multer` modules. Then, it creates an Express app and a Multer instance. The Multer instance is configured to store uploaded files in the `./uploads` directory and to use the file's original name with a timestamp as the filename.

Next, the app creates a route that listens on the `/upload` path. This route uses the `upload.single("file")` middleware to handle file uploads. When a file is uploaded, the middleware stores the file in the `./uploads` directory and then sends a response to the client indicating that the file was uploaded successfully.

Finally, the app listens on port 3000 and logs a message to the console when the server starts.

To run this code, you can create a new Express project and install the `multer` package. Then, you can copy and paste the code above into a file called `app.js`. Finally, you can start the Express development server and view the route in your browser.


---

Original Source: https://www.mindstick.com/forum/159300/create-an-express-js-route-to-upload-files-to-the-server-using-multer-middleware

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
