-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontatosBackend.js
More file actions
58 lines (45 loc) · 1.62 KB
/
contatosBackend.js
File metadata and controls
58 lines (45 loc) · 1.62 KB
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
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
var contatos = [
{_id: 1, nome: "Bruno", telefone: "9999-2222", data: new Date(), operadora: {nome: "Oi", codigo: 14, categoria: "Celular"}},
{_id: 2, nome: "Sandra", telefone: "9999-3333", data: new Date(), operadora: {nome: "Vivo", codigo: 15, categoria: "Celular"}},
{_id: 3, nome: "Mariana", telefone: "9999-9999", data: new Date(), operadora: {nome: "Tim", codigo: 41, categoria: "Celular"}}
];
var operadoras = [
{nome: "Oi", codigo: 14, categoria: "Celular", preco: 2},
{nome: "Vivo", codigo: 15, categoria: "Celular", preco: 1},
{nome: "Tim", codigo: 41, categoria: "Celular", preco: 3},
{nome: "GVT", codigo: 25, categoria: "Fixo", preco: 1},
{nome: "Embratel", codigo: 21, categoria: "Fixo", preco: 2}
];
app.listen(process.env.PORT || 3412);
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get('/contatos', function(req, res) {
res.json(contatos);
});
var ID_INCREMENT = 3;
app.post('/contatos', function(req, res) {
var _contato = req.body;
_contato._id = ++ID_INCREMENT;
contatos.push(_contato);
res.json(true);
});
app.get('/operadoras', function(req, res) {
res.json(operadoras);
});
app.get('/contatos/:id', function(req, res){
var _id = req.params.id;
var contato = contatos.filter(function(aux){
if (aux._id == _id){
return aux;
}
})[0];
res.json(contato);
});