-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
107 lines (83 loc) · 2.84 KB
/
server.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
const express = require('express');
const app = express();
const handlebars = require("express-handlebars")
const dataModel = require("./datamodels/dataModel")
const mongoose = require('mongoose');
const port = 4069
var db = `airports`
var host = `mongodb://localhost/${db}`
const App = {
init: function () {
// const schema = { name: String, age: Number }
const schema = { name: String }
const Location = mongoose.model('location', schema);
// Location.find({name: "Olkhovka Airport"}).then((e, data) => {
// console.log(e)
// })
function generateNewLocation(nameParam) {
if (nameParam === "") throw new Error("name must be filled")
new Location({ name: nameParam }).save((err) => {
if (err) {
} else {
console.log("done!")
}
})
}
try {
generateNewLocation("")
}
catch (e) {
console.log("An error has ocurred")
console.log(e)
}
Location.find({ name: /sofi/gi }, (err, locations) => {
locations.forEach((location) => {
console.log(' --> location: ', location.name);
});
});
// Cat.findByIdAndUpdate("5c519ce5c769b26d0b2e1714", {name: "mileydys", age: Math.random()*10}).then(e => {
// console.log(e)
// })
// const kitty = new Cat({ name: 'inés', age: 12 });
// kitty.save((err) => {
// if (err) {
// console.log(err);
// } else {
// console.log('meow');
// }
// });
////////////////////////////////////////////////////////////////////
//as per the learning unit
//app.set('views', __dirname + '/views');
//here we set the templating default engine
app.set('view engine', 'hbs');
//todo: indicate origin URL
app.engine('hbs', handlebars({
extname: 'hbs',
defaultLayout: 'layout',
layoutsDir: __dirname + '/views',
partialsDir: __dirname + '/views/partials'
}));
app.get('/', (request, response, next) => {
const viewData = dataModel()
response.render("index", viewData)
});
app.get('/carlos', (request, response, next) => {
const viewData = dataModel()
var section
if (Math.random() * 100 > 50) {
section = "carlos"
} else {
section = "dani"
}
response.render(section, viewData)
});
app.listen(port)
console.log(`Listening on port ${port}`)
}
}
mongoose.connect(host);
mongoose.connection.on('connected', function () {
console.log(`Connected to ${host}`)
App.init()
})