diff --git a/exercises/nodejs/02-interactive/index.js b/exercises/nodejs/02-interactive/index.js index 2efda33..13e1feb 100755 --- a/exercises/nodejs/02-interactive/index.js +++ b/exercises/nodejs/02-interactive/index.js @@ -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(); @@ -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( [{ 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)); 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 //////////////