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

Показать описание
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
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
Комментарии