-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
27 lines (26 loc) · 941 Bytes
/
Copy pathapp.js
File metadata and controls
27 lines (26 loc) · 941 Bytes
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
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
const url = req.url;
const method = req.method
if (url === '/') {
res.write('<html>')
res.write('<head><title>My First Page</title></head>')
res.write('<body><form action="/message" method="POST"><input type="text"><button type="submit">send</button></input></form></body>')
res.write('</html>')
return res.end();
}
if (url === '/message' && method === 'POST') {
fs.writeFileSync('message.txt', 'DUMMY')
res.statusCode = 302
res.setHeader('Location', '/');
return res.end();
}
res.setHeader('Content-Type', 'text/html')
res.write('<html>')
res.write('<head><title>My First Page</title></head>')
res.write('<body><h1> Hello from my Node.js Server</h1></body>')
res.write('</html>')
res.end();
})
server.listen(3000);