-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (52 loc) · 1.53 KB
/
server.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
55
56
57
58
59
60
61
var http = require('http');
var fs = require('fs');
var url = require("url");
var path = require("path");
const { types } = require('util');
var port = '8080';
function onRequest(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('index.html',null, function(error, dataa) {
if (error) {
response.writeHead(404);
response.write('File not found');
} else {
// emitter.emit('massageLogged');
response.write(dataa);
}
response.end();
});
if(request.url == "/add" && request.method == "POST"){
let body = '';
request.on('data', chunk => {
body += chunk.toString('utf8')
})
request.on('end', function () {
fs.readFile('./data.json', 'utf8', (err, jsonString) => {
if (err) {
console.log("File read failed:", err)
return;
}else{
console.log("File read succeeded:", jsonString+body)
}
try {
fs.writeFile('./data.json', jsonString+body+"\n", err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})
} catch(err) {
console.log('Error parsing JSON string:', err)
}
})
})
request.on('error', err=> {
console.log("recieve err" + err)
})
}
}
http.createServer(onRequest).listen(8080, function(){
console.log("Server is running on port: " + port);
});