This is error which am getting while post data and file. I have followed \'academind\' tutorial for building Restful API services, also i have been searching answer for this
You don't have permission to access /uploads/ on this server.
Try the following:
sudo chmod -R 777 /uploads
Note: In order to remove all special character, we can use replace function as
const cleanVariable = mixSpecialCharters.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
I had a similar error and this is how I resolved it. After using the replace method, I changed './uploads/images/' to 'uploads/images'. In this case, multer created the folder automatically. So you have something like this
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads/');
},
filename: function(req, file, cb) {
cb(null, new Date().toISOString().replace(/:/g, '-')+ file.originalname);
}
});
For Windows users.
I think if you work with Windows OS, you should use another methods of Date().i
write code like this:
filename:(req,file,cb)=>{ cb(null,new Date().toDateString()+file.originalname) }
Try the following:
Change this line
cb(null, './uploads/');
With this:
cb(null, path.join(__dirname, '/uploads/'));
As I can see, you are trying to get a path that is not on served on the server, but rather a path that is on the server machine.
UPDATE
Try also changing this
app.use('/uploads', express.static('uploads'));
To this:
app.use(express.static(__dirname));
In order to expose the __dirname for static files.
You should change the file name. Because ':' is not allowed in Windows.
Eg:
const storage = multer.diskStorage({
destination: function(req, file, cb){
cb(null,'./uploads/');
},
filename: function(req,file,cb){
cb(null, new Date().toISOString().replace(/:/g, '-') +'-'+ file.originalname);
}
});