-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
34 lines (33 loc) · 1008 Bytes
/
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
const fs = require('fs');
const requestHandler = (req , res) => {
const url = req.url;
const method = req.method
if(url ==='/'){
res.setHeader('content-type','text/html');
res.write("<html>")
res.write("<head><h1>HELLO</h1></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();
}
if(url === '/message' && method =='POST'){
const body = [];
req.on('data' , chunk => {
console.log(chunk);
body.push(chunk);
});
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
const message = parsedBody.split('=')[1]
console.log(message)
fs.writeFileSync('message.txt', message);
})
res.statusCode = 302;
res.setHeader('location' , '/')
return res.end();
}
}
module.exports = {
handler : requestHandler ,
someText : "hello routes js"
};