How to use Multer without express - node.js

I am trying to set up image upload for a website with a node.js backend. I am sending the image file as a FormData object on a ajax post request. Can I use Multer without Express to upload the images? If so, how can I do it? (considering that I am not using express I have to manually collect post data)

Do not use multer, use busboy instead.
multer is only a wrapper around busboy to stick into express' middleware system. Multer is however not strcitly required to be used with express, it just is about to fit into express.
So you could use multer by writing some "unwrapping" code. But it is easier to use busboy without unwrapping the wrapping.
There are straightforward examples on the busboy npm page.

Related

Why use multer rather than manually upload your files from controller

So im new on using expressjs, usually i choose Laravel as my backend. but because some certain consideration, i choose expressjs.
On Laravel, when we handling file upload, we can write upload logic everywhere, its your freedom to do that. You can encapsulate it inside your model function, or put it on service, or anywhere you want.
But when i use expressjs, so many articles on internet that recommend us to use multer for upload your file. As my background is using Laravel previously, i found its weird to use multer. Its because multer is a middleware. Why on earth we use middleware to upload our images/files.
With this i cant encapsulate my business logic into one service and its make the code separated and with this thats mean i need to maintain one business logic from multiple place.
Could you explain me why everyone choose multer ?
why dont just upload it to our local storage manually ?( actually for now i dont know how to do this ).
What is pros on mins from using this library ?
multer is a body parsing middleware that handles content type multipart/form-data
That means it parses the raw http request data which are primarily used for file upload, and makes it more accessible (storing on disk / in memory /...) for further processing.
Without multer, you would have to parse the raw data yourself if you want to access the file.
With this i cant encapsulate my business logic into one service and
its make the code separated and with this thats mean i need to
maintain one business logic from multiple place. Could you explain me
why everyone choose multer ?
multer, just like other middlewares, can be used at the root for all routes, but can also be put on only specific routes too.
More on express middleware
First of all, Express/body-parser does not handle file uploads natively, so that is why you see other libraries being loaded to handle them. They are all going to be loaded as middleware so they can be injected into the request and handle that a file was actually uploaded.
Coming from a Symfony background, I understand where you are coming from with wanting to handle things more manually, as I do the same. There are other alternatives to multer; for example I use express-fileupload which allows you to load the the uploading middleware for your entire app, and then you can use req.files to handle your uploads. For example:
// load the file upload library as app middleware
const fileUpload = require('express-fileupload');
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
}));
Then let's say you have a file input named 'foo':
<input name="foo" type="file" />
In your route you would handle it like so:
// now handle a file upload
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
The file-upload documentation has examples for the req.files object as well as options you can pass to the middleware itself.

Any downside to using Multer middleware on my whole express app?

Early on in my Express app, I define Multer middleware to be used like this:
const multer = require('multer');
app.post('*', multer({ storage: multer.memoryStorage() }).any());
I'm using multer for file uploads.
The majority of my post request to my app will not require any file uploads and so that middleware is mostly useless most of the time.
Is there any downside or danger to using it like this, or is there some reason why I should just be applying the middleware to post requests that require file uploads?
I would suggest you to not use multer as a middleware for the whole app.
Using multer as a middleware would just be acting as a security threat for your app.
For example. You had two routes:
i. /register (requires an image upload as avatar)
ii. /login (doesnt require any file upload)
Any malicious user could easily use /login to upload malicious files into your destination folder in the app and could possibly act as a huge threat.
Only using the multer middleware in routes where you would do a file upload helps mitigate the risk to uploading unwanted and/or malicious files into the server.
Hope this helps.

how to handle post data in client on express

I am using express.js to built the website. i use .ejs as my front end and nodejs for backend i want ejs to interact with the nodejs server. I use get request and i split the url and taking the data but its no where good so i want to interact ejs file to nodejs for eg when we are using php we will try to code as $_POST['somename'] and even we do dynamic programming in php by taking the data and embedding html and writing it. i want to know to handle post request in ejs file and store the post request data and handle it throughout the file
As far as I understood you want to handle your form data, and in order to do so you have to use body-parser.
npm install body-parser
then in your app.js/server.js just add these lines
let bodyParser = require("body-parser")
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
after that you will be able to get $_POST['name_here']
app.post("/whatever", (request, respone){
console.log(request.body.name_here) //same as $_POST['name_here']
})

Is there a way to upload an image and serve that image using mean.io nodeJS express?

I have head that nodejs is not optimal for long running operations. however I would like my overall application to be based on mean.io (express and nodejs). Even though node is not ideal for this operation since its long running, I would still like to use a nodejs module for uploading the image and serving it so i don't have multiple service technologies. I don't want to switch to Java or .net for this operation, just because its not an ideal nodejs task. Is there any reason node would not work for this? In particular, how can I load images to folder belonging to a particular user in mean.io application? thanks
User multer . multer is a body parser for express.
To upload multiple files, user multer.any().
You express code should look like :
var multer = require('multer');
var multerObj= multer({dest : 'local folder dest'});
app.use('/endpoint for upload/*',multerObj.any());
//express code
app.post('/endpoint for upload',function(req,res){
//files cotain the file (binary) as well as the metadata related to the files.
var files = req.files
}

node express upload file with mean stack

I should implement an upload form
I thought of using bodyparser but I read
http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html
so what's the way to upload a file with express using the mean stack ?
may be formidable or other modules ?
That warning is specifically against adding the express.bodyparser middleware to your entire stack as it adds express.multipart to all POST endpoints and therefore file uploads are automatically accepted at all POST endpoints. By default the framework automatically saves any uploaded files to /tmp and so unless you are cleaning them up an attacker could flood your disk with uploaded files.
If you want to avoid using additional modules, what you should do is implement express.multipart on the endpoint(s) where you want to allow file uploads. Here's what I'm talking about:
var express = require("express")
, app = express();
// middleware (no bodyparser here)
app.use(express.json());
app.use(express.urlencoded());
// average GET endpoint
app.get("/", function(req,res) {
res.send('ok');
});
// average POST endpont
app.post("/login", function(req,res) {
res.send('ok');
});
// File upload POST endpoint
app.post('/upload', express.multipart, function(req, res) {
//File upload logic here
//Make sure to delete or move the file accordingly here, otherwise files will pile up in `/tmp`
});
Note the inclusion of express.multipart in the file upload endpoint. This endpoint will now process multipart file uploads, and assuming you handle them correctly they won't be a threat.
Now, having told you all of this, Connect is moving to deprecate multipart due to this exact issue, but there don't seem to be any plans to add a stream based file upload replacement. What they instead recommend is that you use node-multiparty which uses streams to avoid ever placing a file on disk. However, there don't seem to be any good references I can find for using multiparty as a middleware without saving files though, so you'll have to contact the author of multiparty or take a closer look at the API for implementing it with Express.
I created an example that uses Express & Multer - very simple, avoids all Connect warnings
https://github.com/jonjenkins/express-upload

Resources