-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
212 lines (160 loc) · 5.19 KB
/
server.js
File metadata and controls
212 lines (160 loc) · 5.19 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// server.js
// BASE SETUP
// ======================================================
// call the packages that we need
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
//var documentClient = require("documentdb").DocumentClient;
//var config = require("./config");
var DocumentDBClient = require('documentdb').DocumentClient
// , config = require('./config')
// , fs = require('fs')
// , async = require('async')
// , databaseId = config.names.database
// , collectionId = config.names.collection
// , dbLink
// , collLink;
//var host = config.connection.endpoint;
//var masterKey = config.connection.authKey;
// Establish a new instance of the DocumentDBClient to be used throughout this demo
//var client = new DocumentDBClient(host, { masterKey: masterKey });
//var HttpStatusCodes = { NOTFOUND: 404 };
var databaseUrl = `dbs/database1`;
var collectionUrl = `${databaseUrl}/colls/collection1`;
var port = process.env.PORT || 8090; // set the PORT
// configure the app to use body parser()
// this will let us get data from POST
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
var dbConfig = { endPoint: "", peimaryKey: "", databaseId: "", collectionId: "" };
dbConfig.endPoint = "https://localhost:8081/";
dbConfig.peimaryKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
dbConfig.databaseId = "Test";
dbConfig.collectionId = "Test";
var client = new DocumentDBClient(dbConfig.endPoint, {
masterKey: dbConfig.peimaryKey
},
{
DisableSSLVerification :true,
EnableEndpointDiscovery : false,
MediaReadMode : "Buffered",
RequestTimeout : 10000,
MediaRequestTimeout : 10000,
PreferredLocations : [],
RetryOptions: {}
});
// Function definitions
// https://github.com/Azure-Samples/azure-cosmos-db-documentdb-nodejs-getting-started/blob/master/app.js
function getDocument(id) {
let documentUrl = `${collectionUrl}/docs/${id}`;
console.log(`Getting document:${id}`);
console.log(`Collection url: ${collectionUrl}`);
console.log(`Document url: ${documentUrl}`);
client.readDocument(documentUrl, function(err, document){
if (err) {
return console.log(err);
} else {
console.log(document);
}
});
};
// creates the database if it doesn't exist
function getDatabase() {
console.log(`Getting database:\n${dbConfig.databaseId}\n`);
return new Promise((resolve, reject) => {
client.readDatabase(databaseUrl, (err, result) => {
if (err) {
if (err.code == HttpStatusCodes.NOTFOUND) {
client.createDatabase(dbConfig.databaseId, (err, created) => {
if (err) reject(err)
else resolve(created);
});
} else {
reject(err);
}
} else {
resolve(result);
}
});
});
}
function listDocuments(collLink, res){
var querySpec = {
query: 'SELECT * FROM root'
};
// querySpec).toArray(function (err, results) {
// if (err) {
// callback(err);
//
// } else {
// callback(null, results[0]);
// }
// });
client.readDocuments(collectionUrl, querySpec).toArray(function(err, result) {
if(err){
res.json(err);
}
else{
console.log(result);
res.json(result);
}
});
}
console.log(getDatabase());
var id = '00001'
console.log(getDocument(id));
//() => getDocument(id);
// ROUTES for the API
// ======================================================
var router = express.Router();
router.get('/', function(req,res){
res.json({message: 'Welcome to the Snake River Bowl API.'})
});
router.get('/test', function(req,res){
//2.
dbLink = 'dbs/' + dbConfig.databaseId;
console.log(dbLink);
collLink = dbLink + '/colls/' + dbConfig.collectionId;
console.log('\n2. listDocuments in collection \'' + collLink + '\'');
listDocuments(collLink,res);
});
router.post('/', function(req,res){
console.log(req.body);
client.createDocument(collectionUrl,req.body, function(err,created){
if(err){
return err
}
else{
res.json(created);
}
});
});
router.put('/:itemID', function(req,res){
console.log(req.body);
console.log(req.params.itemID)
var self = this;
let documentUrl = `${collectionUrl}/docs/${req.params.itemID}`;
console.log(`Getting document:${id}`);
console.log(`Collection url: ${collectionUrl}`);
console.log(`Document url: ${documentUrl}`);
client.readDocument(documentUrl, function(err, document){
if (err) {
return console.log(err);
} else {
client.replaceDocument(document._self, req.body, function (err, replaced) {
if (err) {
return err;
} else {
return replaced;
}
});
}
});
});
// REGISTER Our ROUTES ----
// all of our routes will be prefixed with /API
app.use('/api', router);
// start the server
app.listen(port);
console.log('Server is started up on port: ' + port);