Multer not uploading files to directory and no errors showing - node.js

I'm trying to get Multer to save images in an src/uploads directory. Everything seems to work fine with no errors but the src/uploads directory is empty after the endpoint is called.
I'm using a microservices architecture using Node, Express, and running it locally DockerDesktop + Kubernetes using Skaffold.
Here is my route:
const router = express.Router();
let storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, './uploads'));
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
let upload = multer({ storage });
router.post(
'/api/products/images',
upload.single('image'),
async (req: Request, res: Response) => {
console.log(req.file);
res.status(201).send('success');
},
);
export { router as imageRouterRouter };
My app file includes:
app.use(imageRouterRouter);
app.use('/uploads', express.static(__dirname));
I know I'm receiving the file successfully via postman, Multer is 'uploading' it and the path is correct as I'm logging the following req.file:
[products] {
[products] fieldname: 'image',
[products] originalname: 'cute-dog.jpeg',
[products] encoding: '7bit',
[products] mimetype: 'image/jpeg',
[products] destination: '/app/src/uploads',
[products] filename: 'cute-dog.jpeg',
[products] path: '/app/src/uploads/cute-dog.jpeg',
[products] size: 18473
[products] }
Any help would be much appreciated as I've spent a couple days on it with no luck. Thanks.

I figured out what the problem was.
I was checking the src/uploads directory on my LOCAL machine, expecting it to contain the new files. But because I'm using docker, the new files were written to the container instead.
Using the below, I explored the container and confirmed the files were there:
docker exec -it <container_id> /bin/sh
Hope this helps anyone else who is as silly as me.

Related

Directory folder to store document

I am trying to make document management project using nodejs,mongodb and react. I want to make directory folder that will store document and the sub is it file. But I could not find any reference that I could use to start making the directory. Is there any specific term that I should search to get any reference to start the project? Thank you.
You can use Multer library for file uploads. It's super easy to use.
import multer from 'multer';
//specify where you want your files to be stored
var fileStorageEngine = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './images')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '--' + file.originalname)
}
})
var upload = multer({ storage: fileStorageEngine });
//pass it as a middleware to a route where you expect to get a file upload
app.use('/api/file/', upload.single('image'), someRouterHere);

Node.js does multer with diskStorage create a folder if it doesn't exist?

I have a small app that receives via POST a file and stores it in a specific folder.
I know that this line:
const multer = multer({ dest: ‘media' })
(of course with some more code, where I use the multer.single('somefilename')) will create a new folder named media if it doesn't exist already.
But I want to be able to control the name given to the file and some other stuff, so I'm using it with diskStorage instead:
const x = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'media')
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
})
const multer = multer({ storage: x })
(there's some more code of course with multer.single('somefilename'))
It works fine when the folder 'media' already exists, but doesn't create it if it doesn't - is it supposed to, or only the simpler multer can do that?
Thanks in advance!
It won't create the folder, they warn that in the official doc:
Note: You are responsible for creating the directory when providing
destination as a function. When passing a string, multer will make
sure that the directory is created for you.
Below is an example of how to create the entire path, but exist many alternatives:
const path = require("path");
const shell = require('shelljs');
const fullPath = path.join(__dirname, '..', '..', "uploads");
shell.mkdir('-p', fullPath);
As #robertklep suggest, please consider: fs.mkdir() instead of shell:
fs.mkdir(path.join(__dirname, 'test'), { recursive: true })

Node: multer change destination folder for fields

I have simple multer image uploading.
// Multer settings
// STORAGE FOR USER AVATAR
var storageAvatar = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/avatars/')
},
filename: function (req, file, cb) {
cb(null, req.user.id + '.jpg')
}
})
// STORAGE FOR ARTICLE THUMBNAILS
var storageThumbnail = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/thumbnails/')
},
filename: function (req, file, cb) {
cb(null, "clanok" + '.jpg')
}
})
// SETTING UPLOAD FOLDER
var upload = multer({
storage: storageAvatar
})
// Multer BEFORE CSRF!!!
app.use(upload.fields([{
name: 'avatar',
maxCount: 1,
}, {
name: 'thumbnail',
maxCount: 1,
}]));
my problem is that i am unable to set different folder for avatars and thumbnails. I can set only one folder for both :/ Anything else i tried ends with invalid CSRF. Thanks for anny suggestions.
EDIT: in one form i am using only one thing. So for example in profile update i have only possibility to change avatar in article adding i have only ability to change thumbnail of article. They not need to bey in upload fields can be seperate but dont know how.
I found on forum way how to go over csrf problem by adding ?_csrf={{csrfToken}} to the end of action in form so i can use official way of using multer :)

Multer isn't saving to specified destination

I'm fairly new to nodejs/ express, but no matter what I seem to do I cant seem to get multer to save to the specified destination, it seems to completely ignore the parameter all together. The code is shown below
//app.js
var multer = require('multer');
var fs = require('fs');
var apiRouter = express.Router();
var app = express();
var store = multer.diskStorage({
filename: function(req,file,cb){
console.log("filename");
cb(null, Date.now()+'.'+file.originalname);
},
desitnation: function(req,file,cb){
console.log("storage");
cb(null,'./public/');
}
});
var upload = multer({storage:store}).single('file');
apiRouter.post('/upload', function(req, res){
upload(req, res, function (err) {
if (err) {
return res.end(err.toString());
}
console.log(req.file);
return res.json({originalname:req.file.originalname, uploadname:req.file.filename});
});
});
The response I get when uploading is shown below:
GET /vendor.js.map 200 3.916 ms - 6636755
filename
{ fieldname: 'file',
originalname: 'Desert.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\\Users\\Dwyer\\AppData\\Local\\Temp',
filename: '1538979138829.Desert.jpg',
path:
'C:\\Users\\Dwyer\\AppData\\Local\\Temp\\1538979138829.Desert.jpg',
size: 845941 }
POST /api/upload 200 70.031 ms - 69
It seems to be setting the file correctly, but I'm not sure where it gets the destination from, no3 do I understand why the destination parameter isn't being read.
Its actually "destination" (not desitnation). You should also make sure that you have a folder ,with the specified name, at the specified destination.
Do you try using physical address in destination? I guess destination in multer document is physical address in linux os.
but no matter what I seem to do I cant seem to get multer to save to the specified destination, it seems to completely ignore the parameter all together.
This is because there seem to be a typo in the configuration.
'use strict';
var store = multer.diskStorage({
filename: function (req, file, cb) {
console.log("filename");
cb(null, Date.now() + '.' + file.originalname);
},
destination: function (req, file, cb) { // it is destination not desitnation :)
console.log("storage");
cb(null, './public/');
}
});

multer file upload hangs with express on server but not locally

I have been searching a while but not got answer please help
So I want to upload files to server, I have code
var upload = multer({ storage: common.storage , fileFilter : common.fileFilter,limits:{fileSize:config.maxSize} }).array('media');
upload(req, res, function (err) {
if (err) {
res.json({'success' : false , 'err' : err , msg : 'Something went wrong please try again'});
}else{
if(req.fileValidationError) {
return res.end(req.fileValidationError);
}`
Its some code to upload file on multer where common.storage function is as
'storage' : multer.diskStorage({
destination: function (req, file, cb) {
console.log(req.pathToSaveFile)
cb(null, 'uploads/' + req.pathToSaveFile)
},
filename: function (req, file, cb) {
console.log('file' , file)
cb(null, req.timestamp +'_'+ file.originalname)
},
}),
after console file nothing happens, please help me in this
`
If your server is running with PM2 than its the most common error we got, because of watch here is an example for file upload with node js.
const multer = require('multer');
var storage = multer.diskStorage({server
destination: function (req, file, callback) {
callback(null, 'UPLOAD_FOLDER_PATH');
},
filename: function (req, file, callback) {
callback(null, new Date().toISOString().replace(/[-T:\.Z]/g, "") + file.originalname) ; // toISOString has been used to rename your file.
}
});
var upload = multer({ storage : storage}).single('YOUR_INPUT_FIELD_NAME');
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
} else {
// File uploaded successfully.
// Do you stuff.
}
Note : There are two way to use for the following code with PM2
1) Start PM2 server without watch. (Not recommended.)
pm2 start index.js
2) Start PM2 server with watch and ignore file upload folder.
pm2 start index.js --watch --ignore-watch "UPLOAD_FOLDER_PATH"
Happy coding cheers!

Resources