forked from Lotti/to-do-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (97 loc) · 3.05 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//------------------------------------------------------------------------------
// Deployment tracking
//------------------------------------------------------------------------------
require('cf-deployment-tracker-client').track();
const express = require('express');
const cfenv = require('cfenv');
const app = express();
const bodyParser = require('body-parser');
// load local VCAP configuration
let vcapLocal = null;
try {
vcapLocal = require('./vcap-local.json');
console.log("Loaded local VCAP", vcapLocal);
} catch (e) {
console.log("Can't find local VCAP");
}
const appEnvOpts = vcapLocal ? {
vcap: vcapLocal
} : {};
const appEnv = cfenv.getAppEnv(appEnvOpts);
let db;
if (process.env.DB_MEMORY === "true") {
db = require('./lib/in-memory')();
} else {
if (appEnv.services['cloudantNoSQLDB']) {
db = require('./lib/cloudant-db')(appEnv.services['cloudantNoSQLDB'][0].credentials);
} else if (appEnv.services['compose-for-mongodb']) {
db = require('./lib/compose-db')(appEnv.services['compose-for-mongodb'][0].credentials);
} else {
db = require('./lib/in-memory')();
}
}
console.log('Using', db.type());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json()); // parse application/json
app.use(express.static(__dirname + '/www'));
app.get('/api/todos', (req, res) => {
db.search().then(todos => {
res.send(todos);
}).catch(err => {
res.status(500).send({ error: err });
});
});
app.post('/api/todos', (req, res) => {
db.create(req.body).then(todo => {
res.send(todo);
}).catch(err => {
res.status(500).send({ error: err });
});
});
app.get('/api/todos/:id', (req, res) => {
db.read(req.params.id).then(todo => {
res.send(todo);
}).catch(err => {
res.status(500).send({ error: err });
});
});
app.put('/api/todos/:id', (req, res) => {
db.update(req.params.id, req.body).then(todo => {
res.send(todo);
}).catch(err => {
res.status(500).send({ error: err });
});
});
app.delete('/api/todos/:id', (req, res) => {
db.delete(req.params.id).then(todo => {
res.send(todo);
}).catch(err => {
res.status(500).send({ error: err });
});
});
// connect to the database
db.init().then(() => {
// start server on the specified port and binding host
app.listen(appEnv.port, "0.0.0.0", function () {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
});
module.exports = app;