-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·74 lines (63 loc) · 1.68 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
68
69
70
71
72
73
74
var http = require('http');
var fs = require('fs');
function createList() {
var liste = [];
for (var i = 0; i < 100; i++) {
liste.push(createPoint());
}
return liste;
}
function createPoint() {
var point = {}
point['lat'] = 43+Math.random()*8;
point['long'] = Math.random()*8-1;
return point
}
function createReponse(){
var tableau = {};
tableau['list']=createList();
return tableau;
}
function createRequete(){
var tableau = {};
var alea = Math.floor(Math.random()*10000);
console.log(alea);
var quoi = 'resto'+alea;
var ou = 'Rennes'+alea;
tableau['requete'] = {'quoi':quoi,'ou':ou, 'timestamp':Date.now()};
return tableau;
}
var server = http.createServer(function(req, res) {
console.log(req.url);
if(req.url == '/api/requete'){
res.writeHead(200, {"Content-Type": "text/html"});
res.end(JSON.stringify(createRequete()));
}
else if(req.url == '/api/points'){
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(createReponse()));
}
else if(req.url == '/'){
fichier = req.url.substr(1);
console.log(fichier);
fs.readFile('index.html',function (err, data){
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
res.write(data);
res.end();
});
}
else if(req.url == '/favicon.ico'){
res.writeHead(404, {'Content-Type': 'text/html','Content-Length':0});
res.end();
}
else{
fichier = req.url.substr(1);
console.log(fichier);
fs.readFile(fichier,function (err, data){
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
res.write(data);
res.end();
});
}
});
server.listen(3000);