Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  160] [ 1]  / answers: 1 / hits: 15219  / 9 Years ago, sun, december 13, 2015, 12:00:00

I got a working REST-API built with node.js and Express.



Now I need a file-upload endpoint, which accepts uploaded files and processes them.



I am using an Express Router and some Authentication middleware.



server.js (excerpt)



var router = express.Router()
app.use(/api, router)

[...]
router.use(function(req, res, next) {
//Authentification middleware
[...]
next()
})

router.route(/upload)
.post(function(req, res){
//upload logic
})


How can I use multer to serve the uploaded file as req.file (or so), but only in /api/upload and for authed users?


More From » node.js

 Answers
8

Ok, I got it.



I can use



var multer = require(multer)
var upload = multer({ dest: some/path })

[...]

router.route(/upload)
/* replace foo-bar with your form field-name */
.post(upload.single(foo-bar), function(req, res){
[...]
})

[#64082] Thursday, December 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominics

Total Points: 424
Total Questions: 99
Total Answers: 107

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
dominics questions
Wed, Apr 6, 22, 00:00, 2 Years ago
Thu, Jan 13, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
;