forked from knightsamar/cs340_sample_nodejs_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
planets.js
58 lines (48 loc) · 1.82 KB
/
planets.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
module.exports = function(){
var express = require('express');
var router = express.Router();
function servePlanets(req, res){
console.log("You asked me for some planets?")
var query = 'SELECT planet_id, name, population FROM bsg_planets';
var mysql = req.app.get('mysql');
var context = {};
function handleRenderingOfPlanets(error, results, fields){
console.log(error)
console.log(results)
console.log(fields)
//take the results of that query and store ti inside context
context.planets = results;
//pass it to handlebars to put inside a file
res.render('planets', context)
}
//execute the sql query
mysql.pool.query(query, handleRenderingOfPlanets)
//res.send('Here you go!');
}
function serveOnePlanet(chicken, steak) {
console.log(chicken.params.fancyId);
console.log(chicken.params);
fancyId = chicken.params.fancyId
var queryString = "SELECT planet_id, name, population, language, capital FROM bsg_planets WHERE planet_id = ?"
var mysql = steak.app.get('mysql')
var context = {};
function handleRenderingOfOnePlanet(error, results, fields){
console.log("results are " + results)
context.planet = results[0]
console.log(context)
if(error){
console.log(error)
steak.write(error)
steak.end();
}else{
steak.render('serverPlanet',context);
}
}
//execute the query
var queryString = mysql.pool.query(queryString, fancyId, handleRenderingOfOnePlanet);
//steak.send("Here's a good tasty well done steak");
}
router.get('/', servePlanets);
router.get('/:fancyId', serveOnePlanet);
return router;
}();