Skip to content

Instantly share code, notes, and snippets.

@Swarag-N
Last active June 30, 2020 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swarag-N/053174a19ecfb877c2d7caf6acab14eb to your computer and use it in GitHub Desktop.
Save Swarag-N/053174a19ecfb877c2d7caf6acab14eb to your computer and use it in GitHub Desktop.
This is an example gist to parse form data in node js "without" express js and body-parser
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Node Intial</title>
</head>
<body>
<h1>Hello</h1>
<form action="http://127.0.0.1:3000/" method="post">
<input type="text" name="name" id="">
<input type="email" name="email" id="">
<input type="number" name="num" id="id">
<input type="submit">
</form>
</body>
</html>
//To create a httpserver
const http = require('http');
//to retrive a html file
const fs = require('fs');
//converting the POST string to Object
const qs = require('querystring')
const hostname = '127.0.0.1';
const port = 3000;
delete require.cache['./index.html']
const people = []
function printIf(arr,len){
console.log(`This is ${arr.length} time`);
if (arr.length%5==0){
console.log(arr);
}
}
app = (req, res) => {
if (req.method == "POST"){
let body = '';
req.on('data', chunk => {
body += chunk.toString(); // convert Buffer to string
});
req.on('end', () => {
people.push(qs.parse(body));
printIf(people,5)
res.end('ok');
});
}else{
fs.readFile('./index.html','utf8',(error,data)=>{
if (error) throw error
res.statusCode = 200;
//Since we are rendering the html
res.setHeader('Content-Type', 'text/html');
res.end(data);
})
}
}
const server = http.createServer(app);
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Server running at http://127.0.0.1:3000/
This is 1 time
This is 2 time
This is 3 time
This is 4 time
This is 5 time
[ [Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' } ]
This is 6 time
This is 7 time
This is 8 time
This is 9 time
This is 10 time
[ [Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' },
[Object: null prototype] { name: 'John ', email: 'jhon@gmail.com', num: '7845574812' } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment