-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (64 loc) · 2.17 KB
/
index.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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const config = require('./config.json');
const app = express();
// contact mongo db
mongoose.connect('mongodb://'+config.mongo.host+'/contacts');
// setup application
app.use(bodyParser.json());
app.use(function (req, res, next) {
req.getRequestUrl = function () {
return req.protocol + "://" + req.get('host') + req.path;
};
return next();
});
const contactTranslator = require('./service/ContactTranslator');
const contactModel = require('./schema/contactModel');
const cm = new contactModel();
app.get('/contact', function (req, res) {
var contacts = [];
var q = req.query.q ? req.query.q : null;
var ref = req.query.ref ? req.query.ref : null;
var dbContacts = cm.findByQuery(q, ref, function (dbContacts) {
dbContacts.forEach(function (d) {
contacts.push(new contactTranslator(d).translate(req));
});
res.jsonp(contacts);
});
});
app.get('/contact/:contact_id', function (req, res) {
var id = req.param('contact_id').toString();
var dbContact = cm.findById(id, function (dbContact) {
if (dbContact === null) {
res.status(404).send();
} else {
res.jsonp(new contactTranslator(dbContact).translate(req));
}
});
});
app.post('/contact', function (req, res) {
var body = req.body;
cm.insert(body, function (dbContact) {
res.status(201).jsonp(new contactTranslator(dbContact).translate(req));
});
});
app.put('/contact/:contact_id', function (req, res) {
var body = req.body;
var id = req.param('contact_id').toString();
var contact = cm.update(id, body, function (dbContact) {
return res.jsonp(new contactTranslator(dbContact).translate(req));
});
});
app.delete('/contact/:contact_id', function (req, res) {
var id = req.param('contact_id').toString();
cm.delete(id);
res.jsonp();
});
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});