forked from IBM-Cloud/get-started-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (66 loc) · 2.08 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
var express = require("express");
var app = express();
var cfenv = require("cfenv");
require("cf-deployment-tracker-client").track();
var mydb;
app.get("/addName", function (request, response) {
var userName = request.query.user_name;
if(!mydb) {
console.log("No database.");
response.send("Hello " + userName + "!");
return;
}
// insert the username as a document
mydb.insert({ "userName" : userName }, function(err, body, header) {
if (err) {
return console.log('[mydb.insert] ', err.message);
}
response.send("Hello " + userName + "! I added you to the database.");
});
});
app.get("/getNames", function (request, response) {
var names = [];
if(!mydb) {
response.json(names);
return;
}
mydb.list({ include_docs: true }, function(err, body) {
if (!err) {
body.rows.forEach(function(row) {
if(row.doc.userName)
names.push(row.doc.userName);
});
response.json(names);
}
});
});
// load local VCAP configuration and service credentials
var vcapLocal;
try {
vcapLocal = require('./vcap-local.json');
console.log("Loaded local VCAP", vcapLocal);
} catch (e) { }
const appEnvOpts = vcapLocal ? { vcap: vcapLocal} : {}
const appEnv = cfenv.getAppEnv(appEnvOpts);
if (appEnv.services['cloudantNoSQLDB']) {
// Load the Cloudant library.
var Cloudant = require('cloudant');
// Initialize database with credentials
var cloudant = Cloudant(appEnv.services['cloudantNoSQLDB'][0].credentials);
// Create a new "mydb" database.
cloudant.db.create('mydb', function(err, data) {
if(err) //If database already exists
console.log("err creating db. It might already exist.");
//console.log(err);
else
console.log("Created database.");
});
// Specify the database we are going to use (mydb)...
mydb = cloudant.db.use('mydb');
}
//serve static file (index.html, images, css)
app.use(express.static(__dirname + '/views'));
var port = process.env.PORT || 3000
app.listen(port, function() {
console.log("To view your app, open this link in your browser: http://localhost:" + port);
});