---
title: "Discussion on \"Build a REST API with Node.js: Upload an Image File\" | Hashnode"
description: "Discussion on \"Build a REST API with Node.js: Upload an Image File\". Hello everyone! Welcome back to the 5th part of Let's Build a Node.js REST API Series. We are so close to finishing this API. Let's not waste any more time and begin!\nIf you are new to this series, please check out the previous articles to follow alo..."
url: "https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Build a REST API with Node.js: Upload an Image File](https://blog.lo-victoria.com/build-rest-api-with-nodejs-upload-files-mongodb?utm_source=hashnode&utm_medium=feed)

Hello everyone! Welcome back to the 5th part of Let's Build a Node.js REST API Series. We are so close to finishing this API. Let's not waste any more time and begin! If you are new to this series, please check out the previous articles to follow alo...

Aug 7, 2020 · 5 min read · blog.lo-victoria.com  
Updated Mar 31, 2021  
Read the article

-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#rest-api](https://hashnode.com/tag/rest-api)
-   [#mongodb](https://hashnode.com/tag/mongodb)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#beginners](https://hashnode.com/tag/beginners)

## Written by

[Victoria Lo](https://hashnode.com/@victoria)

@victoria · Singapore

  
[View profile](https://hashnode.com/@victoria)

A nerd in books, tea, games and software.

I'm a solutions engineer lead, GitHub Star, Director of WomenDevsSG, and co founder of ragTech. I work at the intersection of tech, systems, and leadership, and this blog is where I share my journey through all three. Expect honest reflections, real experiences, and thoughts that are still forming rather than polished career advice.

## Responses 11

Markdown supported · ⌘↵ to post

[TEAM SCITS](https://hashnode.com/@teamscits)

[Sep 7, 2023](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/64f94652c035cd988c3239d0)

HI. thanks for the amazing tutorial. But when I'm using it on server it's not working. Localhost is working . Why is it?

[Harkirat Singh](https://hashnode.com/@Harkirat99)

[Jul 28, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/61012a6f2db92534dc932b52)

is any tutorial available to get that image in angular

[Harkirat Singh](https://hashnode.com/@Harkirat99)

[Jul 28, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/610115c82db92534dc93292d)

can anyone provide github link i am little bit confused

[M. Çağlar TUFAN](https://hashnode.com/@n3pix)

[May 10, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/60997a061ea29f2c341e36b5)

Hey there! That's a good Express REST API tutorial. I came here for only upload part of the application and I found this useful, thank you for your effort. I have a question if you spare few minutes. I implemented the same logic in my application. The task that my router should do is called after uploadImage middleware called, so If any error occurs in my router due to validation, but the application still uploads the image. I removed uploadImage middleware and did the upload part inside my router if validations are passed succesfully. Is this the correct method? And lastly, I couldn't see any way to implement async/await for upload method into the uploadImage logic, I checked out multer's API and couldn't find a method that does the same job but returns Promise. My current implementation has a callback hell, I'm not pretty happy about it. What do you think about it? Thanks in advance and keep amazing work! :)

[Chuong Tang](https://hashnode.com/@ChuongTang)

[Apr 23, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/60821e12ff5fc65284a7709e)

Love it! Thanks for the very thorough tutorial .

[Victoria Lo](https://hashnode.com/@victoria)

[Apr 23, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/60821e12ff5fc65284a7709e#reply-608240cfff5fc65284a77331)

Thanks for reading! Feel free to ask questions if you are unsure of anything 😃

[Sidharth Kumar](https://hashnode.com/@Sidk)

[Feb 2, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/6019606f58cc670f3df464e5)

Hi [Victoria Lo](https://hashnode.com/@victoria) , Awesome tutorial.Its really very good tut. I have problem while using image upload from postman. Only when i use form-data method 'post' in POSTMAN i'm getting error while using image type file. Getting this error name": "ValidatorError", "message": "Path `name` is required.". But all the pervious code with out image is working fine with raw data with json data type. I try to debuge it .It happens only when i use form-data then in code req.body.name is not working but while using raw data using json its working. Should i need to use this

```
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))

app.use(bodyParser.json())
```

![Screenshot from 2021-02-02 19-52-51.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1612275806337/b_5lSpLBS.png) Can you please help me upload image using form-data using POSTMAN ?

[Victoria Lo](https://hashnode.com/@victoria)

[Feb 2, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/6019606f58cc670f3df464e5#reply-60196c1d58cc670f3df465dc)

Hi Sidharth, please check your routes carefully and make sure it's just `/tea` and POSTman is not expecting a `/tea/:name`, which may cause the error.

Another possibility is that the `image` property in the `newTea` function is still `req.body.image`. Please update it to `req.file.path` like so (if you haven't):

```
const newTea = new Tea({
     name:req.body.name,
     image: req.file.path,  //update this
     description: req.body.description,
     keywords: req.body.keywords,
     origin: req.body.origin,
     brew_time: req.body.brew_time,
     temperature: req.body.temperature,
})
```

[Sidharth Kumar](https://hashnode.com/@Sidk)

[Feb 2, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/6019606f58cc670f3df464e5#reply-60196fe158cc670f3df4660c)

Hi [Victoria Lo](https://hashnode.com/@victoria),

Its working. Thanks a lot, Simple error on my side on routes. The way you explain the tutorial is amazing. Thanks once again for your support.

[Victoria Lo](https://hashnode.com/@victoria)

[Feb 2, 2021](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/6019606f58cc670f3df464e5#reply-601972fb0394d00f62dc4827)

Glad it's all good now [Sidharth Kumar](https://hashnode.com/@Sidk)! Feel free to ask any questions if there's anything you're unsure of.

Thanks for reading and following along the series! Hope you'll get to build your own amazing API after this :)

[Fernando Torres](https://hashnode.com/@fertorresmx)

[Sep 28, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f718850eb14b172f1d4b9d5)

Hi again!

There's a typo on Step 3 on this post. It's a missing closing parenthesis.

The code should be:

```
const uploadImg = multer({storage: storage}).single('image');
```

[Rajat Verma](https://hashnode.com/@rajat2502)

[Aug 19, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f3d5b108e81ee3421654cde)

Amazing one!!!

[Richard Harris](https://hashnode.com/@richard788)

[Aug 7, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f2d3c073b12e25afe3e469b)

👏👏👏

[Bolaji Ayodeji](https://hashnode.com/@bolajiayodeji)

[Aug 7, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f2cf5153b12e25afe3e4495)

Yet another amazing one! 🤩

[Victoria Lo](https://hashnode.com/@victoria)

[Aug 7, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f2cf5153b12e25afe3e4495#reply-5f2d42dd3b12e25afe3e46bc)

Many thanks Bolaji 🙏

[Subha Chanda](https://hashnode.com/@nemo)

[Aug 7, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f2cd9a7b8ac395b1f239d03)

Awesome! Are you going to cover JWT?

[Victoria Lo](https://hashnode.com/@victoria)

[Aug 7, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f2cd9a7b8ac395b1f239d03#reply-5f2d4acc3b12e25afe3e4701)

I was planning to... but I wanted to make it simple for beginners so this series only covers the basics on how to build an API.

Thanks for the input. I'll write a separate article on ways to protect API routes in detail if you don't mind :)

[Subha Chanda](https://hashnode.com/@nemo)

[Aug 7, 2020](https://hashnode.com/posts/build-rest-api-with-nodejs-upload-files-mongodb/5f2cb59d3b12e25afe3e42df/comment/5f2cd9a7b8ac395b1f239d03#reply-5f2d4e153b12e25afe3e473a)

Awesome! 😃 [Victoria Lo](https://hashnode.com/@victoria)
