-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex1.js
51 lines (48 loc) · 1.23 KB
/
index1.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
//cau8
const http = require('http');
const {parse} = require('querystring');
const server = http.createServer((req, res) =>{
if(req.method ==='POST'){
RequestData(req, result => {
console.log(result);
res.end(`Data: ${result.fname}`);
});
}
else {
res.end(`
<!doctype html>
<html>
<body>
<form action="/" method="post">
<input type="text" name="fname" /><br />
<button>Save</button>
</form>
</body>
</html>
`);
}
});
server.listen(3000);
function RequestData(request, callback) {
const FORM_URLENCODED = 'application/x-www-form-urlencoded';
if(request.headers['content-type'] === FORM_URLENCODED) {
let body = '';
request.on('data', chunk => {
body += chunk.toString();
});
request.on('end', () => {
callback(parse(body));
});
}
else {
callback(null);
}
}
//cau9
// if(req.headers["x-access-token"] == "nextsolution") {
// res.statusCode = 200;
// res.end('Welcome')
// } else {
// res.statusCode = 404;
// res.end('acceess denied')
// }