Node.js

217 readers
1 users here now

founded 1 year ago
MODERATORS
1
5
submitted 2 months ago by lysdexic to c/nodejs
2
 
 

When I submit the form twice, three times, etc. I get duplicated form field data. Here is the console output of the form data after every submissions.

Also all the field values are always an array, even if the input field for that value is a text input field? Not sure if this is another issue or not.

Here is my code. Only one JS file and one HTML file...

import fs from 'fs';
import http from 'http';

import { formidable as formidablePackage } from 'formidable';

const port = 8080;

//Will create horizontal line that will fit the width of the terminal window
const horizontalLine = '='.repeat(process.stdout.columns);

const formidable = formidablePackage({
	allowEmptyFiles: true,
	minFileSize: 0,
});

http
	.createServer(async (request, response) => {
		if (request.method === 'POST') {
			let [formFieldData, formFileData] = await formidable.parse(request);

			console.log(formFieldData);
		}

		response.setHeader('Content-Type', 'text/html');

		fs.createReadStream('form.html').pipe(response);
	})
	.listen(port);
<form method="post" enctype="multipart/form-data">
	<input name="myText" />
	<br />
	<input type="checkbox" name="myCheckboxGroupA" />
	<input type="checkbox" name="myCheckboxGroupA" />
	<input type="checkbox" name="myCheckboxGroupA" />
	<br />
	<input type="file" name="myFileA" />
	<br />
	<input type="file" name="myFileB" multiple />
	<br />
	<input type="file" name="myFileC" multiple directory webkitdirectory />
	<br />
	<input type="submit" />
</form>

These are the console logs

$ node form.js
{ myText: [ 'hello world' ], myCheckboxGroupA: [ 'on' ] }
{
  myText: [ 'hello world', 'hello world' ],
  myCheckboxGroupA: [ 'on', 'on' ]
}
{
  myText: [ 'hello world', 'hello world', 'hello world' ],
  myCheckboxGroupA: [ 'on', 'on', 'on' ]
}
{
  myText: [ 'hello world', 'hello world', 'hello world', 'hello world' ],
  myCheckboxGroupA: [ 'on', 'on', 'on', 'on' ]
}

These are the console logs I expected

$ node form.js
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }
3
4
5
6
submitted 3 months ago* (last edited 3 months ago) by lysdexic to c/nodejs
6
7
4
Node v20.16.0 (nodejs.org)
submitted 4 months ago by mac to c/nodejs
8
9
7
submitted 5 months ago by mac to c/nodejs
10
8
submitted 5 months ago by mac to c/nodejs
11
9
submitted 5 months ago by mac to c/nodejs
12
7
submitted 6 months ago by mac to c/nodejs
13
7
submitted 7 months ago by mac to c/nodejs
14
6
submitted 7 months ago by mac to c/nodejs
15
16
5
submitted 8 months ago by lysdexic to c/nodejs
17
5
submitted 8 months ago* (last edited 8 months ago) by mac to c/nodejs
 
 

The Node.js project will release new versions of the 18.x, 20.x, 21.x releases lines on or shortly after, Tuesday, April 9, 2024 in order to address:

  • 1 high severity issues.
  • 1 medium severity issues.
18
6
submitted 8 months ago* (last edited 8 months ago) by mac to c/nodejs
 
 

Linked in the post is 21.7.2

19
8
submitted 8 months ago* (last edited 8 months ago) by mac to c/nodejs
 
 

The Node.js project will release new versions of the 18.x, 20.x, 21.x releases lines on or shortly after, Wednesday, April 3, 2024 in order to address:

  • 1 medium severity issue.
  • 1 high severity issue.
20
21
6
require(esm) in Node.js (joyeecheung.github.io)
submitted 8 months ago by lysdexic to c/nodejs
22
 
 

cross-posted from: https://programming.dev/post/11927506

Many times when I’ve tried to write some code in Node.js using ES modules I’ve found myself reaching for __dirname and then looking up how to replace it. Now Node.js, Deno and Bun support import.meta.dirname. Check out the article for more detail.

23
24
25
view more: next ›