-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (61 loc) · 1.94 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
'use strict';
const express = require('express');
const Cloudant = require("cloudant");
var bodyParser = require('body-parser');
const port = process.env.PORT;
const app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
if (process.env.VCAP_SERVICES) {
var vcap = JSON.parse(process.env.VCAP_SERVICES);
var credentials = vcap.cloudantNoSQLDB[0].credentials ;
} else {
var credentials = {
"host": process.env.cloudanthost,
"password": process.env.cloudantpassword,
"port": process.env.cloudantport,
"url": process.env.cloudanturl,
"username": process.env.cloudantusername
}
}
//app.use(bodyParser);
var cloudant = Cloudant(credentials, function(er, cloudant, reply) {
if (er) throw er
console.log('Connected with username: %s', reply.userCtx.name)
});
cloudant.db.list(function(err, allDbs) {
if (err) {
return console.log('Failed to initialize Cloudant: ' + err.message);
}
console.log('All my databases: %s', allDbs.join(', '))
});
var db_certif = cloudant.db.use('db_certif_bluemix');
app.get('/', function(req,res) {
res.send('Hello');
console.log("I send Hello once");
});
app.get('/getTeamBelgiumDoc', function(req,res) {
db_certif.get('ce419ef8ab82173a615b0b877a2ba7b5', { revs_info: true }, function(err, body) {
if (!err) {
console.log(body);
res.send(body.team_name);
} else {
res.send('did not find it :( ');
}
});
});
app.get('/user/:id', function (req, res, next) {
res.end(req.params.id)
});
app.post('/postexample', function (req, res) {
//console.log(req.body.test);
//var test = req.json();
var tata = req.body.test;
db_certif.insert({ crazy: true, test: tata }, function(err, body) {
if (!err)
console.log(body)
})
res.send("over");
});
app.listen(port);
console.log("I listen on port "+ port);