-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_reader.js
87 lines (79 loc) · 3.44 KB
/
math_reader.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var http = require('http');
var fs = require('fs');
var port=100
var server = http.createServer(function(요청,응답){
var _url = 요청.url;
var _method = 요청.method;
console.log(decodeURIComponent(_url),_method,요청.connection.remoteAddress);
if(_url=='/'){
fs.readFile('index.html','utf-8',(E,파일)=>{
var 확장자 = 'text/html; charset=utf-8'
응답.writeHead(200, {'Content-Type':확장자} );
응답.end(파일);
})
}
else if(_url=='/edit'){
fs.readFile('edit.html','utf-8',(E,파일)=>{
var 확장자 = 'text/html; charset=utf-8'
응답.writeHead(200, {'Content-Type':확장자} );
응답.end(파일);
})
}
else if (_url=='/html'){
fs.readdir('./files',(E,파일목록)=>{
var 데이터='[';
for (var i=0; i<파일목록.length; i++){
데이터+=`"${파일목록[i]}",`;
}
데이터+=']'
var 확장자 = 'text/html; charset=utf-8';
응답.writeHead(200, {'Content-Type':확장자,'Accept-Ranges': 'bytes', 'Content-Length': Buffer.byteLength(데이터, 'utf8').toString()} );
응답.end(데이터);
})
}
else if (_url.includes('files/') && _url.indexOf('files/')==1){
fs.readFile("files/"+decodeURIComponent(_url.substr("files/".length)),'utf-8',(E,파일)=>{
//console.log(E,파일);
var 확장자 = 'text/html; charset=utf-8'
응답.writeHead(200, {'Content-Type':확장자} );
응답.end(파일);
})
}
else if (_url.includes('js/') && _url.indexOf('js/')==1){
fs.readFile("js/"+decodeURIComponent(_url.substr("js/".length)),(E,파일)=>{ //이부분 'utf-8'로 읽었다가 설정 안 되는 오류 발생했었음
console.log(E);
var 확장자 = 'text/html; charset=utf-8'
if (_url.includes('woff')) 확장자='application/font-woff'
//console.log(파일.length)
응답.writeHead(200, {'Content-Type':확장자, 'Content-Length': 파일.length.toString()} );
응답.end(파일);
})
}
else if(_url=='/save' && _method=='POST'){
var post_data = '';
요청.on('data', (data) => {
post_data += data;
});
요청.on('end', () => {
var Filename = post_data.substr('name='.length, post_data.indexOf('text=')-7)
var Filedata = post_data.substr(post_data.indexOf('text=')+5)
console.log('이름:',Filename);//,'데이터:',Filedata
console.log('32')
fs.writeFile('./files/'+Filename+'.txt', Filedata, (E) => {
console.log('파일', E);
var 확장자 = 'text/html; charset=utf-8'
응답.writeHead(200, {'Content-Type': 확장자});
응답.end("<script>alert('성공')\nlocation='/'</script>");
});
})
}
else{
console.log("404 방금 수상했음");
var 확장자='text/html; charset=utf-8';
응답.writeHead(404, {'Content-Type':확장자} );
응답.end("404");
}
})
server.listen(port, function(){
console.log(`Server is running at localhost:${port}`);
});