From d32bc20e4bace4475cf94e03807f64cbe52d44a8 Mon Sep 17 00:00:00 2001 From: Fantin VERPILLOT Date: Fri, 18 Mar 2016 11:04:53 +0100 Subject: [PATCH 1/4] exercise 2 --- exercises/nodejs/02-interactive/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/exercises/nodejs/02-interactive/index.js b/exercises/nodejs/02-interactive/index.js index 2efda33..a59fc2c 100755 --- a/exercises/nodejs/02-interactive/index.js +++ b/exercises/nodejs/02-interactive/index.js @@ -16,6 +16,7 @@ const chalk = require('chalk'); const prompt = require('prompt'); // https://www.npmjs.com/package/prompt const valueToGuess = _.random(1, 100); +console.log(valueToGuess); prompt.start(); @@ -29,9 +30,14 @@ prompt.get([{ console.log(`Welcome, ${result.name} !`); - tryAgain(function() {}); + tryAgain(callback); }); +function callback(arg1, arg2) { + if (!arg2) { + tryAgain(callback); + } +} function tryAgain(callback) { prompt.get( [{ From ad0d02920f1e4a3506da925e688be6a299725063 Mon Sep 17 00:00:00 2001 From: Fantin VERPILLOT Date: Fri, 18 Mar 2016 11:54:10 +0100 Subject: [PATCH 2/4] correct exercise 2 --- exercises/nodejs/02-interactive/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/exercises/nodejs/02-interactive/index.js b/exercises/nodejs/02-interactive/index.js index a59fc2c..62e9f7a 100755 --- a/exercises/nodejs/02-interactive/index.js +++ b/exercises/nodejs/02-interactive/index.js @@ -33,10 +33,14 @@ prompt.get([{ tryAgain(callback); }); -function callback(arg1, arg2) { - if (!arg2) { +function callback(err, hasWon) { + if (err) return console.error(err); + if (!hasWon) { tryAgain(callback); } + else { + prompt.stop(); + } } function tryAgain(callback) { From b51876528861380345a8e8146361663ccec35c04 Mon Sep 17 00:00:00 2001 From: Fantin VERPILLOT Date: Fri, 18 Mar 2016 12:16:53 +0100 Subject: [PATCH 3/4] exercise 5 --- .../nodejs/05-express_routing/api/index.js | 71 ++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/exercises/nodejs/05-express_routing/api/index.js b/exercises/nodejs/05-express_routing/api/index.js index 243238a..46588d2 100755 --- a/exercises/nodejs/05-express_routing/api/index.js +++ b/exercises/nodejs/05-express_routing/api/index.js @@ -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(` + + + meta routes + + + +

...

+
  • ${req.baseUrl}/photos +
  • ${req.baseUrl}/facebook + + + `); }); +router.get('/photos', function (req, res) { + res.send(` + + + meta routes + + + +

    Photos

    + + + `); +}); - -// TODO one or two routes -// be creative ! - - +router.get('/facebook', (req, res) => { + res.send(` + + + meta routes + + +Redirection...
    + + + `); +}); ////////////////// examples ////////////// From 6ca43852cd53b5b766a8d6211f1a17d369d75d9e Mon Sep 17 00:00:00 2001 From: Fantin VERPILLOT Date: Mon, 28 Mar 2016 00:55:27 +0200 Subject: [PATCH 4/4] correct num2 + do num3 --- exercises/nodejs/02-interactive/index.js | 2 -- exercises/nodejs/03-promise/index.js | 38 +++++++----------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/exercises/nodejs/02-interactive/index.js b/exercises/nodejs/02-interactive/index.js index 62e9f7a..13e1feb 100755 --- a/exercises/nodejs/02-interactive/index.js +++ b/exercises/nodejs/02-interactive/index.js @@ -14,9 +14,7 @@ const _ = require('lodash'); const chalk = require('chalk'); const prompt = require('prompt'); // https://www.npmjs.com/package/prompt - const valueToGuess = _.random(1, 100); -console.log(valueToGuess); prompt.start(); diff --git a/exercises/nodejs/03-promise/index.js b/exercises/nodejs/03-promise/index.js index 5ab1b33..39d4257 100755 --- a/exercises/nodejs/03-promise/index.js +++ b/exercises/nodejs/03-promise/index.js @@ -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); }); }); } @@ -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));