-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·75 lines (63 loc) · 1.93 KB
/
main.js
File metadata and controls
executable file
·75 lines (63 loc) · 1.93 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
const express = require('express');
const path = require('path');
const mustacheExpress = require('mustache-express');
const app = express();
app.engine('mustache', mustacheExpress());
app.set('views', './views')
app.set('view engine', 'mustache')
app.use(express.static(__dirname + '/public'))
const mongodb = require("mongodb");
const MongoClient = mongodb.MongoClient
const url = 'mongodb://localhost:27017/robots';
app.get("/", function(request, respond) {
MongoClient.connect(url, function(error, db) {
if (error) {
throw error;
} else {
console.log("Successfully connected to the database");
}
const data = require("./data");
for (var i = 0; i < data.users.length; i++) {
const user = data.users[i];
db.collection("users").updateOne({
id: user.id
}, user, {upsert: true})
}
db.collection("users").find().toArray(function(error, documents) {
respond.render("robots", {robots: documents})
})
})
})
app.get('/unemployed', function(request,respond){
MongoClient.connect(url, function(error, db) {
if (error) {
throw error;
}
db.collection("users").find({job: null}).toArray(function(error, documents) {
respond.render("robots", {robots: documents})
})
})
})
app.get("/employed", function(request,respond){
MongoClient.connect(url, function(error, db) {
if (error) {
throw error;
}
db.collection("users").find({job: {$nin: [null]}}).toArray(function(error, documents) {
respond.render("robots", {robots: documents})
})
})
})
app.get('/:id', function (request, respond) {
MongoClient.connect(url, function(err, db) {
if (err) {
throw err;
}
db.collection("users").find({id: parseInt(request.params.id)}).toArray(function(err, documents) {
respond.render("individual", {robots: documents})
})
})
})
app.listen(3000, function() {
console.log('Successfully started express application!');
})