expressjs / expressjs/multer

How can I use res on middleware

Open
#967 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

Hello, first I want to thx author.

I use multer on Middleware, here is my code.

```
// /middleware/file.js
const multer = require('multer');

const profileUpload = multer({
limit: {
fileSize: 1000000
},
fileFilter(req, file, cb) {
if (!file.originalname.match(/\.(jpg|jpeg|png)$/)) {
cb(null, false);
}
cb(null, true);
}
})

module.exports = {
profileUpload
};
```

Q1: I want to use `res.status(401).json({...})` on fileFilter function(I only saw req), it's possibly?

Here is my router code.

```
// /router/file.js

var express = require('express');
var router = express.Router();

const { profileUpload } = require('../../../middleware/file');
const fileController = require('../../../controllers/file');
const fileSchema = require('./schema');

const apiBasePath = `/common/api/v1/file`

/* GET users listing. */
router
.post(`${apiBasePath}/upload-file`, profileUpload.single('profile_img'), fileSchema.request.uploadSchema, fileController.upload)

module.exports = router;
```

If user upload not (jpg|jpeg|png), I want to throw error on fileFilter that `res.status(401).json({...})`.

Bye the way, I use express "~4.16.1".

Thx guys.

Contributor guide

Open the contributing guide

Research direction

The issue shows middleware/file.js and router/file.js; start by reading Multer's fileFilter callback contract and how profileUpload.single('profile_img') passes control to Express. The payload does not identify a target source file, test, or acceptance criteria, so a completed contribution cannot be defined from the issue alone.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, node.js
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.