expressjs / expressjs/multer

How to parse a form but not automatically in case a form element is missing

Open
#1,159 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

I have the following function:

```
app.post("/upload", upload.single('file'), function (request, response, next) {

if (request.file==null) {
response.json("No file selected.");
return;
}
}
```

If the form does not have the file submitted does the call upload.single("file") cause errors? I've included a check to see if the form property exists.

Is there an alternative way to parse the form body?

Can I do something like this?
```
app.post("/upload", function (request, response, next) {
var test = upload.single('file');
if (request.file==null) {
response.json("No file selected.");
return;
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start at the app.post('/upload', upload.single('file'), ...) entry point and inspect how upload.single('file') populates request.file when the multipart form omits the file. Confirm the documented behavior and identify whether an alternative form-body parsing approach is supported; done means providing a clear usage answer for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.