-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (66 loc) · 2.51 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
var http = require('http');
var fs = require('fs'); // to get data from html file
var client = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/admin';
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
// req.url stores the path in the url
var url = req.url;
if (url === "/") {
// fs.readFile looks for the HTML file
// the first parameter is the path to the HTML page
// the second is the call back function
// if no file is found the function gives an error
// if the file is successfully found, the content of the file are contained in pgres
fs.readFile("head.html", function (err, pgres) {
console.log(pgres)
if (err)
res.write("HEAD.HTML NOT FOUND");
else {
var url = 'mongodb://localhost:27017/admin';
client.connect(url, { useNewUrlParser: true }, function (err, db) {
if (err) {
console.log("Connection failed")
}
console.log("Connected to mongoDB");
var dbo = db.db("test")
var cursor = dbo.collection('test').find();
cursor.each(function (err, doc) {
if (doc != null)
console.log(doc);
});
db.close();
});
}
});
}
else if (url === "/tailPage") {
fs.readFile("tail.html", function (err, pgres) {
if (err)
res.write("TAIL.HTML NOT FOUND");
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(pgres);
res.end();
}
});
}
}).listen(5000, function () {
console.log("SERVER STARTED PORT: 5000");
});
//dbconnect*********************************************************************************************************
// var client = require('mongodb').MongoClient;
// var url = 'mongodb://localhost:27017/admin';
// client.connect(url, { useNewUrlParser: true }, function (err, db) {
// if (err) {
// console.log("Connection failed")
// }
// console.log("Connected to mongoDB");
// var dbo = db.db("test")
// var cursor = dbo.collection('test').find();
// cursor.each(function (err, doc) {
// if (doc != null)
// console.log(doc);
// });
// db.close();
// });