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 Aboubakar #13

Open
wants to merge 2 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
6 changes: 5 additions & 1 deletion exercises/nodejs/02-interactive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ prompt.get([{

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

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


Expand Down
70 changes: 70 additions & 0 deletions exercises/nodejs/03-promise/#index.js#
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/sh
':' //# http://sambal.org/?p=1014 ; exec /usr/bin/env node --harmony_modules --harmony_regexps --harmony_proxies --harmony_sloppy_function --harmony_sloppy_let --harmony_unicode_regexps --harmony_reflect --harmony_destructuring --harmony_default_parameters --harmony_sharedarraybuffer --harmony_atomics --harmony_simd "$0" "$@"
'use strict';

/**
* FETCHING AN API
*
* Fetch the StarWars API http://swapi.co/
*
*/


const _ = require('lodash');
const inquirer = require('inquirer'); // https://www.npmjs.com/package/inquirer
const fetch = require('node-fetch'); // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
const ora = require('ora'); // https://github.com/sindresorhus/ora
const prettyjson = require('prettyjson');


// admire how it's readbale :
askUser()
.then(fetchData)
.then(displayResults)
.catch((err) => {
console.error('! Something bad happened :');
console.error(err);
});


function askUser() {
return new Promise(function (fulfill, reject) {
inquirer.prompt([
{
name: 'dataType',
type: 'list',
message: 'What do you want to know about ?',
default: 'people',
choices: ['films', 'people', 'planets', 'species', 'starships', 'vehicles']
},
{
name: 'id',
message: 'Which id ? (1-n)',
default: '9'
}
], function (choices) {
console.log(choices);

// TODO resolve the promise !!!
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
});
});
}

function fetchData(choices) {
console.log('fetchData input :', choices);

const url = 'http://swapi.co/api/' + choices.dataType + '/' + choices.id;
console.log(url);

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 !'));
}

function displayResults(data) {
console.log('result :\n', prettyjson.render(data));
}
25 changes: 25 additions & 0 deletions exercises/nodejs/04-express_hello_world/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
':' //# http://sambal.org/?p=1014 ; exec /usr/bin/env node --harmony_modules --harmony_regexps --harmony_proxies --harmony_sloppy_function --harmony_sloppy_let --harmony_unicode_regexps --harmony_reflect --harmony_destructuring --harmony_default_parameters --harmony_sharedarraybuffer --harmony_atomics --harmony_simd "$0" "$@"
'use strict';

// a minimal "hello world" express app

console.log('Hello world !');

const _ = require('lodash');
const express = require('express');

const local_ips = require('../../../src/server/common/local-ips').getLocalIps();

const listening_port = 3000;

// http://expressjs.com/4x/api.html
const app = express();

app.get('/', (req, res) => res.send('hello world'));

console.log('(Ctrl+C to stop)');

app.listen(listening_port, () => {
local_ips.forEach(ip => console.log('Listening on http://' + ip + ':' + listening_port));
});
66 changes: 66 additions & 0 deletions exercises/nodejs/05-express_routing/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

// http://offirmo.net/wiki/index.php?title=Express.js
// http://expressjs.com/4x/api.html

const _ = require('lodash');
const express = require('express');


/////////////////////////////////////////////

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
});



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


router.get('/MyApi', function (req, res) {
res.send(`
<!DOCTYPE html>
<head>
<title>meta routes</title>
<style type="text/css">
body {
margin: 20px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
color: #444;
}
</style>
</head>
Yeaaaah ça marche
`);
});

router.get('/MyApi2', function (req, res) {
res.send(`www.9gag.com`);
});


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

router.get('/hello/:name', function (req, res) {
res.send(`Hello, ${req.name} !`);
});


router.get('/stuff/:id', function (req, res) {

res.status(500).json({ error: 'not implemented !' })

/*
res.type('json').send({
id: req.id
});
*/
});
45 changes: 45 additions & 0 deletions exercises/nodejs/05-express_routing/api/index.js~
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

// http://offirmo.net/wiki/index.php?title=Express.js
// http://expressjs.com/4x/api.html

const _ = require('lodash');
const express = require('express');


/////////////////////////////////////////////

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
});



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



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

router.get('/hello/:name', function (req, res) {
res.send(`Hello, ${req.name} !`);
});


router.get('/stuff/:id', function (req, res) {

res.status(500).json({ error: 'not implemented !' })

/*
res.type('json').send({
id: req.id
});
*/
});
44 changes: 44 additions & 0 deletions exercises/nodejs/05-express_routing/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh
':' //# http://sambal.org/?p=1014 ; exec /usr/bin/env node --harmony_modules --harmony_regexps --harmony_proxies --harmony_sloppy_function --harmony_sloppy_let --harmony_unicode_regexps --harmony_reflect --harmony_destructuring --harmony_default_parameters --harmony_sharedarraybuffer --harmony_atomics --harmony_simd "$0" "$@"
'use strict';

// a better express app

console.log('Hello world !');

const _ = require('lodash');
const express = require('express');

const local_ips = require('../../../src/server/common/local-ips').getLocalIps();

const listening_port = 3000;


/////////////////////////////////////////////

const api_router = require('./api');
const meta_router = require('../../../src/server/common/meta-routes');

/////////////////////////////////////////////

const app = express();

app.use((req, res, next) => {
console.log(`Request to $(req.url) received at`, Date.now());
res.header('x-received-at', Date.now()); // set a custom header
next();
});

app.get('/', function(req, res) {
res.send('hello from app ! Try /meta /api');
});

app.use('/api', api_router);
app.use('/meta', meta_router);

/////////////////////////////////////////////

app.listen(listening_port, () => {
local_ips.forEach(ip => console.log('Listening on http://' + ip + ':' + listening_port));
});
console.log('(Ctrl+C to stop)');
Empty file.