Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kevin Herrbach PR #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion exercises/nodejs/02-interactive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ prompt.get([{

console.log(`Welcome, ${result.name} !`);

tryAgain(function() {});
tryAgain(function(n, b) {again(n, b)});
});


function again(n, b)
{
if (!b)
tryAgain(again);
}

function tryAgain(callback) {
prompt.get( [{
name: 'value',
Expand Down
14 changes: 12 additions & 2 deletions exercises/nodejs/03-promise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function askUser() {
}
], function (choices) {
console.log(choices);

resolve(choices);
// TODO resolve the promise !!!
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
});
Expand All @@ -60,9 +60,19 @@ function fetchData(choices) {
const spinner = ora('Fetching StarWars API...');
spinner.start();

return fetch(url)
.then(function onResponse(response) {
if(response.ok)
{
spinner.stop();
return response.json();
}
else
throw new Error('Network response was not ok.');
});
// TODO now use the fetch API :
// https://developer.mozilla.org/fr/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful
return Promise.reject(new Error('fetchData not implemented !'));
//return Promise.reject(new Error('fetchData not implemented !'));
}

function displayResults(data) {
Expand Down
82 changes: 79 additions & 3 deletions exercises/nodejs/05-express_routing/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,89 @@ const router = module.exports = new express.Router();
/////////////////////////////////////////////

router.get('/', (req, res) => {
res.send('hello from API sub-router !');
res.send(`
<!DOCTYPE html>
<head>
<title>meta routes</title>
<style type="text/css">
body {
margin: 40px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
color: #333;
}
</style>
</head>
<h1>NBA TEAMS API</h1>
<li><a>${req.baseUrl}/spurs</a>
<li><a>${req.baseUrl}/warriors</a>
<li><a>${req.baseUrl}/lakers</a>
<li><a>${req.baseUrl}/knicks</a>
<li><a>${req.baseUrl}/clippers</a>
<li><a>${req.baseUrl}/76ers</a>
<li><a>${req.baseUrl}/bulls</a>
<li><a>${req.baseUrl}/hawks</a>
<li><a>${req.baseUrl}/celtics</a>


<script>
Array.prototype.forEach.call(document.querySelectorAll('a'), function(el) {
el.href || (el.href = el.text);
});
</script>

`);

// TODO a small page listing your endpoints
// cf. js-class-2016-episode-2\src\server\common\meta-routes.js
});



var teams = [
{name : 'Spurs', city: 'San Antonio', numberOfTitles : '5', currentCoach : 'Greg Popovitch', stadium : 'AT&T Center'},
{name : 'Warriors', city: 'Oakland', numberOfTitles : '4', currentCoach : 'Steve Kerr', stadium : 'Oracle Arena'},
{name : 'Lakers', city: 'Los Angeles', numberOfTitles : '16', currentCoach : 'Byron Scott', stadium : 'Staples Center'},
{name : 'Knicks', city: 'New York', numberOfTitles : '2', currentCoach : 'Kurt Rambis', stadium : 'Madison Square Garden'},
{name : 'Clippers', city: 'Los Angeles', numberOfTitles : '0', currentCoach : 'Doc Rivers', stadium : 'Staples Center'},
{name : '76ers', city: 'Philadelphia', numberOfTitles : '3', currentCoach : 'Brett Brown', stadium : 'Wells Fargo Center'},
{name : 'Bulls', city: 'Chicago', numberOfTitles : '6', currentCoach : 'Fred Hoiberg', stadium : 'United Center'},
{name : 'Hawks', city: 'Atlanta', numberOfTitles : '1', currentCoach : 'Mike Budenholzer', stadium : 'Philips Arena'},
{name : 'Celtics', city: 'Boston', numberOfTitles : '17', currentCoach : 'Brad Stevens', stadium : 'TD Garden'},
];

router.get('/spurs', function(req, res){
res.json(teams[0]);
})

router.get('/warriors', function(req, res){
res.json(teams[1]);
})

router.get('/lakers', function(req, res){
res.json(teams[2]);
})

router.get('/knicks', function(req, res){
res.json(teams[3]);
})

router.get('/clippers', function(req, res){
res.json(teams[4]);
})

router.get('/76ers', function(req, res){
res.json(teams[5]);
})

router.get('/bulls', function(req, res){
res.json(teams[6]);
})

router.get('/hawks', function(req, res){
res.json(teams[7]);
})

router.get('/celtics', function(req, res){
res.json(teams[8]);
})
// TODO one or two routes
// be creative !

Expand Down