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

Pull request TP 2 (verpil_f) #9

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
12 changes: 10 additions & 2 deletions exercises/nodejs/02-interactive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
const _ = require('lodash');
const chalk = require('chalk');
const prompt = require('prompt'); // https://www.npmjs.com/package/prompt

const valueToGuess = _.random(1, 100);

prompt.start();
Expand All @@ -29,9 +28,18 @@ prompt.get([{

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

tryAgain(function() {});
tryAgain(callback);
});

function callback(err, hasWon) {
if (err) return console.error(err);
if (!hasWon) {
tryAgain(callback);
}
else {
prompt.stop();
}
}

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

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

// 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 fetch(url)
.then(function(res){
spinner.stop();
if(res.ok) {
return res.json();
} else {
return console.log('ERROR: Network response was not ok.');
}
});
}

function displayResults(data) {
console.log('result :\n', prettyjson.render(data));
}


function getUrl () {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("http://swapi.co/people/3"), 1500)
})
}

getUrl()
.then(function fetchData(url) {
return fetch(url)
.then(function onResponse(response) {
if(response.ok)
return response.json();
else
throw new Error('Network response was not ok.');
});
})
.then(function displayResults(data) {
console.log(data)
})
.catch(err => console.error(err));
71 changes: 63 additions & 8 deletions exercises/nodejs/05-express_routing/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,72 @@ const router = module.exports = new express.Router();
/////////////////////////////////////////////

router.get('/', (req, res) => {
res.send('hello from API sub-router !');
// TODO a small page listing your endpoints
// cf. js-class-2016-episode-2\src\server\common\meta-routes.js
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>...</h1>
<li><a>${req.baseUrl}/photos</a>
<li><a>${req.baseUrl}/facebook</a>

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

router.get('/photos', function (req, res) {
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>Photos</h1>
<img src="http://vignette3.wikia.nocookie.net/ssb/images/2/2b/Lol-face.gif/revision/latest?cb=20100823094728" />

`);
});


// TODO one or two routes
// be creative !


router.get('/facebook', (req, res) => {
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>
Redirection...<br />

<script>
window.location = 'http://www.facebook.fr';
</script>
`);
});

////////////////// examples //////////////

Expand Down