-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
54 lines (47 loc) · 1.75 KB
/
routes.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
const fs = require('fs');
requestHandler = (req, res) => {
const url = req.url;
const method = req.method;
if (url === '/') {
res.write('<html>');
res.write('<head><title>Enter Message</title></head>');
res.write('<body><form action="/message" method="POST"><input type="text" name="message"/> <button type="submit">Send</button></form></body>');
res.write('</html>');
return res.end(); //end at this point due to end()
}
if (url === '/message' && method === 'POST') {
//get request data before writing to the file
const body = [];
//fetch data chunks
req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk);
});
//work on the data fetched from the post method
return req.on('end', () => {
const parseBody = Buffer.concat(body).toString();
const message = parseBody.split('=')[1];
console.log(parseBody);
fs.writeFile('message.txt', message, (err) => {
res.statusCode = 302;
res.setHeader('Location', '/');
return res.end();
}); // blocks until write is done
});
}
if (url === 'message')
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>First Page: Node</title></head>');
res.write('<body><h1>Hello there this is ABC </h1></body>');
res.write('</html>');
res.end();
};
// module.exports = {
// handler: requestHandler,
// someText: 'Some hard coded text'
// };
// module.exports.handler = requestHandler;
// module.exports.someText = 'Some hard coded text';
exports.handler = requestHandler;
exports.someText = 'Some hard coded text';