-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
47 lines (38 loc) · 1.1 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
var express = require('express');
var bodyParser = require('body-parser');
const Contact = require('./contacts');
var BASE_API_PATH = "/api/v1";
var app = express();
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.send("<html><body><h1>My server</h1></body></html>");
});
app.get(BASE_API_PATH + "/contacts", (req, res) => {
console.log(Date() + " - GET /contacts");
Contact.find({}, (err, contacts) => {
if (err) {
console.log(Date() + "-" + err);
res.sendStatus(500);
} else {
res.send(contacts.map((contact) => {
return contact.cleanup();
}));
}
});
});
app.get(BASE_API_PATH + "/healthz", (req, res) => {
res.sendStatus(200);
})
app.post(BASE_API_PATH + "/contacts", (req, res) => {
console.log(Date() + " - POST /contacts");
var contact = req.body;
Contact.create(contact, (err) => {
if (err) {
console.log(Date() + " - " + err);
res.sendStatus(500);
} else {
res.sendStatus(201);
}
});
});
module.exports = app;