-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
86 lines (80 loc) · 1.96 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
75
76
77
78
79
80
81
82
83
84
85
86
var http = require('http');
var fs = require('fs');
var url = require('url');
var server = http.createServer();
var resp = '';
var mongoloide = require('./mongoUtil');
server.on('request', function(req,res){
if(req.url != '/favicon.ico'){
resp = res;
switch(url.parse(req.url).pathname) {
case '/':
if(req.method == 'GET'){
fs.readFile('./index.html', function (err, data) {
resp.writeHeader(200, {"Content-Type": "text/html"});
resp.write(data);
resp.end();
});
}else{
redirect('405.html');
}
break;
case '/api/alunos':
case '/api/alunos/':
if(req.method == 'GET'){
mongoloide.getAll(redirect);
}else{
redirect('405.html');
}
break;
case '/api/aluno':
case '/api/aluno/':
if(req.method == 'GET'){
var queryObject = url.parse(req.url,true).query;
mongoloide.get(queryObject.nome);
redirect('opa.html');
}else{
redirect('405.html');
}
break;
case '/api/aluno':
case '/api/aluno/':
if(req.method == 'POST'){
mongoloide.getAll();
redirect('opa.html');
}else{
redirect('405.html');
}
break;
case '/api/aluno/{aluno}':
case '/api/aluno/{aluno}/':
if(req.method == 'PUT'){
mongoloide.getAll();
redirect('opa.html');
}else{
redirect('405.html');
}
break;
case '/api/aluno':
case '/api/aluno/':
if(req.method == 'DELETE'){
var queryObject = url.parse(req.url,true).query;
mongoloide.deleta(queryObject.nome);
redirect('opa.html');
}else{
redirect('405.html');
}
break;
default:
redirect('404.html');
break;
}
}
});
server.listen(1234);
console.log("node started...");
var redirect = function(data){
var json = JSON.stringify(data);
resp.writeHead(200, {"Content-Type": "application/json"});
resp.end(json);
};