forked from dennisrshelden/IFC.JSON-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
152 lines (117 loc) · 3.95 KB
/
app.js
File metadata and controls
152 lines (117 loc) · 3.95 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
const express = require('express');
const fs = require('fs');
const serveIndex = require('serve-index')
var path = require('path');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient
const url = require('url');
var db;
const app = express();
app.use(bodyParser({ limit: '50mb' }));
//app.use(bodyParser.json());
app.use(express.static('static')); /* this line tells Express to use the public folder as our static folder from which we can serve static files*/
app.get('/', function (req, res) {
res.sendFile('static/index.html');
});
app.use('/filelist', express.static('static/data'), serveIndex('static/data', { 'icons': true }))
//Establish Connection
MongoClient.connect('mongodb://localhost:27017/mydb', function (err, database) {
if (err)
throw err
else {
db = database;
console.log('Connected to MongoDB');
//Start app only after connection is ready
app.listen(8000, function () {
console.log("Listening on port 8000!")
});
}
});
/* // makes a post entry point at /add for adding new JSON model files into /static/data
app.post('/add', (req, res) => {
var filename = req.body.name + ".json";
var data = req.body.details;
data = '[' + data + ']';
fs.writeFileSync("static/data/" + filename, data);
res.redirect('/draw');
}); */
app.post('/pushFile', function (req, res) {
// Insert JSON straight into MongoDB
console.log('Post! file');
const queryObject = url.parse(req.url, true).query;
var collectionName = "static/data/" + queryObject.collection + ".json";
fs.writeFile(collectionName, JSON.stringify(req.body), (err) => {
if (err) { console.log('error writing file'); throw err; }
console.log('Data written to file');
});
});
app.post('/pushModel', function (req, res) {
// Insert JSON straight into MongoDB
console.log('Post!');
var dbo = db.db("mydb3");
const queryObject = url.parse(req.url, true).query;
var collectionName = queryObject.collection;
console.log("collection name: " + collectionName);
dbo.collection(collectionName).insertMany(req.body, function (err, res2) {
if (err)
res.send('Error');
else
//res.send("Success");
res.status(200).send('Success')
});
});
app.get('/mongodb', function (req, res) {
console.log('Mongo!');
var dbo = db.db("mydb3");
const query = url.parse(req.url, true).query;
var collectionName = query.collectionName;
console.log("collection name: " + collectionName);
var queryString = query.queryString;
if (queryString == "") {
queryString = '{"Class":"Space"}';
}
var queryObjects;
if (queryString) queryObjects = JSON.parse(queryString);
// queryObjects = null;
if (queryObjects) {
//var querywithOBJ = { RepresentationType: "OBJ" };
var querywithOBJ = { $or: [ { RepresentationType: "OBJ" } , queryObjects] };
console.log("queryString: " + JSON.stringify(querywithOBJ));
dbo.collection(collectionName).find(querywithOBJ).toArray(function (err, result) {
if (err) throw err;
res.json(result);
});
}
else {
console.log("No queryString");
dbo.collection(collectionName).find({}).toArray(function (err, result) {
if (err) throw err;
res.json(result);
});
}
});
// lists all the Mongo collections in the Mongo database
app.get('/collectionlist', function (req, res) {
console.log('collectionlist!');
var dbo = db.db("mydb3");
//
dbo.listCollections().toArray(function (err, result) {
if (err) throw err;
//console.log(result);
var resJSON = [];
resJSON.push("");
result.forEach(function (item) {
resJSON.push(item.name);
});
res.json(resJSON);
//db.close();
});
});
/* // makes a post entry point at /add for adding new JSON model files into /static/data
app.post('/add', (req, res) => {
var filename = req.body.name + ".json";
var data = req.body.details;
data = '[' + data + ']';
fs.writeFileSync("static/data/" + filename, data);
res.redirect('/draw');
}); */