/
server.js
68 lines (63 loc) · 2.09 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
var express = require('express');
var path = require('path');
var app = express();
var {home} = require('./views/home.html.js');
var {cat} = require('./views/cat.html.js');
class CatStorage {
constructor() {
this.data = {
cats: [
{
info: {name:'Pigwidgeon', path:'/cat/Pigwidgeon', image:'https://i.imgur.com/ea7WXNDm.jpg'},
growth: [
{date:'05/10/2017', age:'6', weight:750, milk: 'n/a', feedings:3, notes:'n/a', medicalNotes:'gave meds'},
{date:'05/11/2017', age:'6', weight:760, milk: 'n/a', feedings:3, notes:'n/a', medicalNotes:'gave meds'}
]
},
{
info: {name:'Errol', path:'/cat/Errol', image:'https://i.imgur.com/VcXX9Pvm.jpg'},
growth: [
{date:'05/10/2017', age:'6', weight:800, milk: 'n/a', feedings:3, notes:'n/a', medicalNotes:'gave meds'},
{date:'05/11/2017', age:'6', weight:810, milk: 'n/a', feedings:3, notes:'n/a', medicalNotes:'gave meds'}
]
},
{
info: {name:'Hedwig', path:'/cat/Hedwig', image:'http://i.imgur.com/HKQjnXzm.jpg'},
growth: [
{date:'05/10/2017', age:'6', weight:700, milk: 'n/a', feedings:3, notes:'n/a', medicalNotes:'gave meds'},
{date:'05/11/2017', age:'6', weight:710, milk: 'n/a', feedings:3, notes:'n/a', medicalNotes:'gave meds'}
]
}
]
}
}
getCatNames(){
let out = []
for(let i=0; i<this.data.cats.length; i++) {
let cat = this.data.cats[i];
out.push(cat.info);
}
return out;
}
getCatInfo(catName){
let out;
for(let cat of this.data.cats) {
if (cat.info.name === catName) {
out = cat;
}
}
return out;
}
}
const catStore = new CatStorage();
app.use('/styles', express.static(__dirname + '/styles'));
app.get('/', function (req, res) {
res.end(home(catStore.getCatNames()));
});
app.get('/cat/:name', function (req, res) {
var name = req.params.name;
res.send(cat(name, catStore.getCatInfo(name)));
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});