-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·67 lines (51 loc) · 1.64 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
62
63
64
65
66
67
const IP = '127.0.0.1';
const PORT = 3000;
var http = require('http').createServer(serverHandler);
var fs = require('fs');
var url = require('url');
var ext = require('path');
var qs = require('querystring');
function serverHandler(req,res){
//var route = rootfolder + url.parse(req.url).pathname;
//console.log('URL : ' + route);
var filepath = '.'+req.url;
console.log('current file path : '+filepath);
if(filepath === './')
{
filepath = './www/index.html';
}
else
{
filepath = './www'+req.url;
}
console.log('before read file : ' + filepath);
fs.readFile(filepath,function(error, file){
if(error){res.writeHead(404);res.end('File NOT FOUND');return;}
console.log('MIME TYPE :' + ext.extname(filepath));
res.writeHead(200,{'content-type':ext.extname(filepath)});
res.end(file);
});
postORget(req,res);
}
function postORget(req,res){
console.log('METHOD : ' + req.method)
switch(req.method)
{
case "POST":
var bodydata = '';
req.on('data',function(data){
bodydata += data
});
req.on('end',function(){
console.log(qs.parse(bodydata));
var data = qs.parse(bodydata);
res.writeHead(200,{'content-type':'text/json'});
res.end(JSON.stringify(data));
});
break;
case "GET":
break;
}
}
http.listen(PORT,IP);
console.log("server running on " + PORT)