Node JS REST API file upload | Node JS REST API file upload example | File upload NodeJS Subject Key

preview_player
Показать описание
Hello and welcome back to our series on Node.JS tutorials. So, far we have covered: What is a REST API?, How to create REST APIs in Express? and How to GET data from POST request in Node JS?

I have provided the video links in the suggessions. This video teaches how to uploada file or multiple files to the server.
As always, I will use POSTMAN to make API Calls to the server.

Without any further delay, lets start-

This is a simple Node JS project. At line no. 1. It import express module. It creates instance of express so that we could create
routes for the project. Then we have a GET method with slash route. From its handler function, we're returning Hellow World to the request originator.
At last, we are making this application listen on PORT: 3000.
Lets create a new route /upload with POST method where we will write our logic to upload the file.

The file object is supposed to come in:

Lets log it to see what it gives..
It is showing undefined. To parse the multipart data we have to use a third party NPM module. The available modules are-
formiddable, multer and express-fileupload.
I'll use express-fileupload for this demo. The command to install express-fileupload is:

npm i express-fileupload

express-fileupload is added for my project. Lets start using it now-

var fileupload = require('express-fileupload');

We need to use it as a router middleware function. So, use it like as following-

Now, restart the server and see what we gets..
It prints the file object. It contains every information about the file like- name, type, mime-type, size and all.
It has a method inside, mv(which stands for move) using this method you can move your file from one location to another on the server.
The mv method has two parameters: the first one is the location where you want to move the file with a name and second one is the handler.

if(err)
throw err;
success: true,
message: "File uploaded!"
});
})
})

err has some value if there is some problem while uploading the file. err is null and result has some value if the file is uploaded successfully.

Lets open the upload folder to see if the newly upload file is present or not.
one by one.

So, in this video, we learnt about uploading a single/multiple file(s). In the next, video of this series we will upload the files directly to Cloudinary/CDN.

Thank you for listening!!

~-----------~----------------~--------------------~--------------~-------------~
Please like my social media page:

To Get Regular Content Updates-
~-----------~----------------~--------------------~--------------~-------------~
#RenuRawat #node #api #express-fileupload
Рекомендации по теме
Комментарии
Автор

We have developed npm package to help you guys, here is the link


It's an alternative too express-fileupload
With some more options like uploading to S3 bucket.. give it a try and let us know for any feedback!

RenuRawat
Автор

I've been looking EVERYWHERE for something to teach me how to do this that isn't an hour-long video. Thank you so, so much! I'm about to watch your video on uploading photos with Cloudinary next.

kaylathomas
Автор

I rarely comment on any video, but this has been the easiest way to upload a file ever, and I've done it tons of times in other frameworks/languages... thank you !!!

sebastianfarias
Автор

I usually don't comment but all the best for adding captions in kannada

nonehejei
Автор

great man!, Since I have tried *multer* but it won't work for me, and i tried this method ... WOW!!! you gave me a hand to know about how to upload a file with very simple...

premkumar-ixjq
Автор

I'll be trying express-fileupload first time. Thanks

nodejs
Автор

These 6 min's saved me ..thanks a lot

paulsamuel
Автор

You saved my whole day good video ❤️❤️

salmanansari
Автор

Here, is a new video on "Uploading files". Please post your queries and comments here...

RenuRawat
Автор

Love you man, you saved my life <3

muhammadwaleedazam
Автор

Hi renu,
I am using multer, when i am sending the image to the node through POSTMAN and acccessing the req.files it always giving me undefined

yateshchhabra
Автор

can u pls tell me . Once a file is uploaded it automatically displays in a particular webpage and only authorize users can see that file ?? So the only thing a uploading person does is to upload a file .

pankajjoshi
Автор

I am logging req.files and it gives me a null return

ifan
Автор

Using Nodejs I am trying to send a file to an Api as multipart request. I do not have the file but I have a ByteArray (buffer) as well as Base64 string of the file I want to send. How can I send the buffer/Base64 string to an Api that's asking for a multipart request and still get the work done?

MegaSajanmathew
Автор

Can u say me difference between multer and this

saurabhtiwwari
Автор

Hi Renu,
what changes will need to be made if using aws lambda (node js) since there we recieve an event object instead of req and res?
Best,
-R

RameshPatel-tjdf
Автор

It is working fine when I hit this API from the postman. But when I use this API from Browser (front-end), It gives me below error

SyntaxError: Unexpected token - in JSON at position 0
at JSON.parse (<anonymous>)

KEVALPANCHALkev
Автор

thanks, i have a query, suppose, if i upload zip file of size 10Gb, containing videos does this approach works?

mrrockstar
Автор

Nice explaination and nice bgm also .
Can u please tell me that background song name.

yerrapremchandu
Автор

I'm getting this error. [Error: ENOENT: no such file or directory, open 'F:\0001\DOCS\Campus\4th year\EEY6A89-Group
_771Z.png'] . I know there is no issue in the code. could you plz help me?

mathusoothananthanabalarat
join shbcf.ru