-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
194 lines (131 loc) · 3.95 KB
/
app.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
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
const fs = require('fs'),
path = require('path'),
contentTypes = require('./utils/content-types'),
sysInfo = require('./utils/sys-info'),
env = process.env,
express = require('express'),
html = require('html')
let app = express();
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
//----------------dbconfig------------------
var marke = 'marke';
var nalog = 'nalog';
var mongoUrl = 'mongodb://localhost:27017/test';
//---------------------------------------------------
var insertDocument = function(db,collection,data, callback) {
db.collection(collection).insert(
data,
function(err, result) {
if(err)
console.log('failed to insert')
assert.equal(err, null);
console.log("insertiarno u kolekciju naloga");
callback();
}
);
};
var insertiraj = function(mongoUrl,collection,data){
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertDocument(db,collection,data, function() {
db.close();
});
});
}
//-----------------------------------------------------------------
var dohvati = function(kolekcija,query,callback){
MongoClient.connect(mongoUrl, function(err, db) {
console.log('uslo u konekciju :');
console.log(kolekcija);
assert.equal(null, err);
var cursor = db.collection(kolekcija).find({});
var result=[];
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
result.push(doc);
//console.dir(doc); //radi
console.log(doc.ime); //radi
} else {
db.close();
//console.log('izašlo iz konekcije');
callback(result);
}
});
});
}
//setTimeout(console.log(rez),3000);
//console.log(result);
//-----------------------------------------
//insertiraj(url,marke,{});
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
// view engine setup
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.use('/static', express.static('static'));
/// catch 404 and forwarding to error handler
/// error handlers
// development error handler
// will print stacktrace
app.get('/', function(req,res){
console.log('get /');
res.render('index.html');
});
app.get('/nalogunos', function(req,res){
console.log(req.query);
insertiraj(url,nalog,req.query);
res.send({message:'Uspješno unesen servisni nalog'});
});
app.get('/selection', function(req,res){
console.log(req.query.selection);
dohvati(req.query.selection,{},function(result){ //selection(id iz klijenta) /callback result
console.log(result);
if(result.length != 0){
res.send(result);
console.log('poslalo: ' + result);
}
else{
res.send({message : '404'});
}
});
});
// IMPORTANT: Your application HAS to respond to GET /health with status 200
// for OpenShift health monitoring
app.get('/health',function(req,res) {
res.writeHead(200);
res.send();
});
/*
if (url == '/health') {
res.writeHead(200);
res.end();
} else if (url.indexOf('/info/') == 0) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(JSON.stringify(sysInfo[url.slice(6)]()));
} else {
fs.readFile('./static' + url, function (err, data) {
if (err) {
res.writeHead(404);
res.end();
} else {
let ext = path.extname(url).slice(1);
res.setHeader('Content-Type', contentTypes[ext]);
if (ext === 'html') {
res.setHeader('Cache-Control', 'no-cache, no-store');
}
res.end(data);
}
});
}
});
server.listen(env.NODE_PORT || 3000, env.NODE_IP || 'localhost', function () {
console.log(`Application worker ${process.pid} started...`);
});
*/
app.listen(env.NODE_PORT || 8080, env.NODE_IP || 'localhost',function(){
console.log('listen 8080');
});
module.exports = app;