-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsever.js
36 lines (33 loc) · 1.01 KB
/
sever.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
const fs = require('fs');
const http = require('http');
const qs = require('querystring');
const server = http.createServer((req, res) => {
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const formData = qs.parse(body);
const jsonData = JSON.stringify(formData);
fs.writeFile('data.json', jsonData, 'utf8', (err) => {
if (err) {
console.error(err);
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Internal Server Error');
return;
}
console.log('Data saved successfully');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Data saved successfully');
});
});
} else {
res.writeHead(405, {'Content-Type': 'text/plain'});
res.end('Method Not Allowed');
}
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});