Skip to content

Commit a197ff7

Browse files
committed
try formidable
1 parent 7b27c8a commit a197ff7

5 files changed

Lines changed: 105 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ general/html-streaming/last-of-type-test.html
7171
general/html-streaming/so.txt
7272
js/network/socketiyo/examples/built/exampleServerReady.js
7373
js/network/socketiyo/examples/upload
74+
js/network/forms/upload/

js/network/forms/form.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@
1010
<input id="name" name="name">
1111
</label>
1212
<label for="file"> file
13-
<input id="file" name="file" type="file">
13+
<input id="file" name="file" type="file" multiple>
1414
</label>
1515
<button>Submit</button>
1616
</form>
17+
18+
<form action="./submitFormidable" method="POST" enctype="multipart/form-data">
19+
<label> name
20+
<input name="name">
21+
</label>
22+
<label> file
23+
<input name="file" type="file" multiple>
24+
</label>
25+
<button>Submit Formidable</button>
26+
</form>
1727
</body>
1828
</html>

js/network/forms/formHandler.mjs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
// draft
22
import http from "http";
3+
import formidable from "formidable";
34
import fs from "fs";
5+
import url from "url";
6+
import path from "path";
7+
const __filename = url.fileURLToPath(import.meta.url);
8+
const __dirname = path.dirname(__filename);
49

510
const PORT = 8000;
6-
const staticsPath = `./`;
11+
const upload = `upload`
12+
const staticsPath = `/`;
713
const FORM_URLENCODED = `application/x-www-form-urlencoded`;
814
const FORM_BYTES = `multipart/form-data`;
915

1016
const staticResponses = {
1117
[`/form.html`]: {
12-
[`file`]: `${staticsPath}/form.html`,
18+
[`file`]: `${__dirname}${staticsPath}form.html`,
1319
[`Content-Type`]: `text/html`
1420
},
1521
};
@@ -54,7 +60,24 @@ const server = http.createServer((request, response) => {
5460
handleSubmit(request, response);
5561
return;
5662
}
57-
console.log(request.url);
63+
if (request.method === `POST` && request.url === `/submitFormidable`) {
64+
console.log(`make sure to create ${__dirname}/${upload} before`)
65+
const form = formidable({
66+
multiples: true,
67+
uploadDir: `${__dirname}/${upload}`,
68+
keepExtensions: true,
69+
maxFileSize: 10**6,
70+
maxFields: 0, // 0 makes it infinite,
71+
maxFieldsSize: 10 ** 6
72+
});
73+
74+
form.parse(request, (err, fields, files) => {
75+
response.writeHead(200, { 'content-type': 'text/plain' });
76+
response.end(JSON.stringify({ fields, files }, null, 2));
77+
});
78+
return;
79+
}
80+
console.log(request.url);
5881
response.setHeader(`Content-Type`, `text/plain`);
5982
response.writeHead(405);
6083
response.end(`Only use GET please`);

js/network/forms/package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/network/forms/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "forms",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"form": "node formHandler.mjs"
7+
},
8+
"private": true,
9+
"dependencies": {
10+
"formidable": "github:node-formidable/formidable"
11+
}
12+
}

0 commit comments

Comments
 (0)