-
Notifications
You must be signed in to change notification settings - Fork 6
/
express.js
57 lines (46 loc) · 1.41 KB
/
express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express');
// If 'Pechkin' is installed, you can simply "require('pechkin')"
// or import * as pechkin from 'pechkin';
const { parseFormData } = require('../dist/cjs');
const app = express();
function pechkinFileUpload (config, fileFieldConfigOverride, busboyConfig) {
return async (req, res, next) => {
try {
const { fields, files } = await parseFormData(req, config, fileFieldConfigOverride, busboyConfig);
req.body = fields;
req.files = files;
return next();
} catch (err) {
return next(err);
}
}
}
app.post(
'/',
pechkinFileUpload(),
async (req, res) => {
const files = [];
for await (const { stream, field, filename } of req.files) {
// Process files however you see fit...
// Here, streams are simply skipped
stream.resume();
files.push({ field, filename });
}
return res.json({ fields: req.body, files });
}
);
app.get(
'/',
(req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
return res.end(`
<h1>Pechkin File Upload</h1>
<h2>Express</h2>
<form enctype="multipart/form-data" method="post">
<div>File 1 (single): <input type="file" name="file1"/></div>
<input type="submit" value="Upload" />
</form>
`);
}
)
app.listen(8000, () => console.log('Send a multipart/form-data request to localhost:8000 to see Pechkin in action...'));