-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
141 lines (117 loc) · 4.35 KB
/
Copy pathserver.js
File metadata and controls
141 lines (117 loc) · 4.35 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var express = require("express");
var app = express();
var cfenv = require("cfenv");
var bodyParser = require('body-parser')
var request = require('request').defaults({json:true});
// Load the Cloudant library.
var Cloudant = require('cloudant');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
var mydb;
/* Endpoint to greet and add a new book to database.
* Send a POST request to localhost:3000/api/visitors with body
* {
* "id": "value",
* "title": "value",
* "booksubjects": "value",
* "bookpublisher": "value",
* "booklanguage": "value",
......
* }
*/
app.post("/api/visitors", function (request, response) {
var id = request.body.id;
var title = request.body.title;
var booksubjects = request.body.booksubjects;
var bookpublisher = request.body.bookpublisher;
var booklanguage = request.body.booklanguage;
var bookyear = request.body.bookyear;
var bookdate = request.body.bookdate;
var bookcontributor = request.body.bookcontributor;
var bookcollections = request.body.bookcollections;
var bookcallNumber = request.body.bookcallNumber;
if(!mydb) {
console.log("No database.");
response.send("Title of the book " + title + "!");
return;
}
// insert the book into the database
mydb.insert({"_id": id, "BookMeta_Title": title, "BookMeta_Subjects": booksubjects, "BookMeta_Publisher": bookpublisher, "BookMeta_Language": booklanguage, "BookMeta_Year": bookyear, "BookMeta_Date": bookdate, "BookMeta_Contributor": bookcontributor, "BookMeta_Collections": bookcollections, "BookMeta_CallNumber": bookcallNumber},
function(err, body, header) {
if (err) {
return console.log('Error:', err.message);
}
response.send("The book " + title + "! has been added to the database.");
});
});
/**
* Endpoint to get a JSON array of all the documents in the database
* @return An array of all the books
*/
app.get("/api/visitors", function (request, response) {
var books = [];
if(!mydb) {
response.json(books);
return;
}
mydb.list({ include_docs: true }, function(err, body) {
if (!err) {
body.rows.forEach(function(row) {
if(row.doc._id)
books.push(row.doc._id);
books.push(row.doc.BookMeta_Title);
books.push(row.doc.BookMeta_Subjects);
books.push(row.doc.BookMeta_Publisher);
books.push(row.doc.BookMeta_Language);
books.push(row.doc.BookMeta_Year);
books.push(row.doc.BookMeta_Date);
books.push(row.doc.BookMeta_Contributor);
books.push(row.doc.BookMeta_Collections);
books.push(row.doc.BookMeta_CallNumber);
});
response.json(books);
}
});
});
// 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'] || appEnv.getService(/cloudant/)) {
// Load the Cloudant library.
var Cloudant = require('cloudant');
// Initialize database with credentials
if (appEnv.services['cloudantNoSQLDB']) {
// CF service named 'cloudantNoSQLDB'
var cloudant = Cloudant(appEnv.services['cloudantNoSQLDB'][0].credentials);
} else {
// user-provided service with 'cloudant' in its name
var cloudant = Cloudant(appEnv.getService(/cloudant/).credentials);
}
//database name
var dbName = 'bookstores';
// Remove any existing database called "bookstores".
/* cloudant.db.destroy(dbName, function(err) {
if(!err) //err if database doesn't exists
console.log("Created database: " + dbName);
}*/
// Create a new "mydb" database.
/*cloudant.db.create(dbName, function(err, data) {
if(!err) //err if database doesn't already exists
console.log("Created database: " + dbName);
});*/
// Specify the database we are going to use (mydb)...
mydb = cloudant.db.use('bookstores');
}
//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);
});