-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
45 lines (40 loc) · 1.22 KB
/
app.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
const http = require("http");
const fs = require('fs');
//console.log(http);
const server = http.createServer(function(req, res, next) {
//console.log(req.url)
const url = req.url;
const method = req.method;
//console.log(method);
if (url === "/index") {
res.setHeader("Content-Type", "text/html");
res.write("<html>");
res.write("<head><title>Node Workshop</title></head>");
res.write("<body>");
res.write("<h1>NODE WORKSHOP </h1>");
res.write("<form action='/message' method='POST'>");
res.write("<input type='text' placeholder='write here'name='message'>");
res.write("<input type='submit' value='Send'>");
res.write("</form>");
res.write("</body>");
res.write("</html>");
return res.end();
}
if (url === "/message" && method === "POST") {
const body = [];
req.on("data", chunk => {
body.push(chunk);
});
req.on('end',() => {
let passedBody = Buffer.concat(body).toString();
let message = passedBody.split('=')[1];
//console.log(message);
fs.writeFile('message.txt',message,(err)=>{
res.write(message);
res.end();
});
});
}
});
server.listen("7000");
console.log("Hello am listening to port 7000");