I am trying to download any file using the Multer Package . It works fine when I use the following code in server.js file.
var express = require('express'), app = express(), multer = require('multer'); app.configure(function () { app.use(multer({ dest: './static/uploads/', rename: function (fieldname, filename) { return filename.replace(/\W+/g, '-').toLowerCase(); } })); app.use(express.static(__dirname + '/static')); }); app.post('/api/upload', function (req, res) { res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name}); }); var server = app.listen(3000, function () { console.log('listening on port %d', server.address().port); });
I want to store the file in different places. I tried the following code, but it does not work for me.
var express = require('express'), app = express(), multer = require('multer'); app.configure(function () { app.use(multer({ //dest: './static/uploads/', rename: function (fieldname, filename) { return filename.replace(/\W+/g, '-').toLowerCase(); } })); app.use(express.static(__dirname + '/static')); }); app.post('/api/pdf', function (req, res) { app.use(multer({ dest: './static/pdf/'})); res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name}); }); app.post('/api/image', function (req, res) { app.use(multer({ dest: './static/image/'})); res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name}); }); app.post('/api/video', function (req, res) { app.use(multer({ dest: './static/video/'})); res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name}); }); var server = app.listen(3000, function () { console.log('listening on port %d', server.address().port); });
So, if I hit the file http://localhost:3000/api/pdf , it should be stored in the "pdf" folder, if I hit the file http://localhost:3000/api/video , it should be stored in the "video" folder .
Is there a way to achieve this?
Thanks in advance.