Formation Node.JS
by Rodolphe Duterval
Rodolphe Duterval
32 🐳
Dev Web FullStack
Introduce yourself?
-
Why are you here?
-
What are your objectives?
-
How can I help you?
Mes stacks
- HTML 5 / CSS 3
- JS (ES6)
- PHP
- Ruby
- C / C++
- Angular 2 / React / Vue
- Node.js
- MongoDB / MySQL
RULES
- Fautes d’orthographe tolérées
- Mauvaise indentation non tolérée
-
Copiage bête non toléré
- Code en français non toléré
- Bavardage tolérés tant que c'est discret
- On ferme les ordi quand c'est demandé
Conseils
-
Participez
-
Pratiquez chez vous
-
Allez au-delàs de ce qu'on vous demande
-
Si vous n'avez pas compris, dites-le !
-
Dormez
-
ne me prenez pas pour un con
What is NODE.JS ?
NODE.JS IS A JAVASCRIPT RUNTIME
BUILT ON GOOGLE'S OPEN-SOURCE
V8 JAVASCRIPT ENGINE. 🤔
Why & When to use Node.js
NODE.JS PROS
USE NODE.JS
- single-threaded, based on even driven, non-blocking I/O model
- Perfect for building fast and scalabe data-intensive apps.
- Companies like : netflix, uber, paypal, ebay have started using node in production
- API with database behind it ( NoSQL)
- Data streaming
- Real-time chat app
- server-side web app
Install Node.js
go on : http://nodejs.org & install it !
node -v
v13.8.0
mkdir node-farm
Core Modules - Node.JS
//index.js
const hello = 'Hello ESTIAM';
console.log(hello)
// lancer dans votre terminal
// node index.js
Node.JS - FS read & write
//index.js
const fs = require('fs');
// create an folder with 2 files .txt [input, final]
fs.readFileSync('./txt/input.txt', 'utf-8');
const textIn = fs.readFileSync('./txt/input.txt', 'utf-8');
console.log(textIn);
// lancer dans votre terminal
// node index.js
Node.JS - FS read & write
//index.js
const fs = require('fs');
// create an folder with 2 files .txt [input, final]
fs.readFileSync('./txt/input.txt', 'utf-8');
const textIn = fs.readFileSync('./txt/input.txt', 'utf-8');
console.log(textIn);
const textOut = `check this out : ${textIn}\nCreated on ${Date.now()}`;
fs.writeFileSync('./txt/output.txt', 'utf-8');
console.log('File written !')
// lancer dans votre terminal
// node index.js
Synchrone
blocking 👎
Asynchrone
non-blocking 🤟
vs.
Do you know callback Hell 🔥🔥 ?
fs.readdir(source, function (err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function (filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function (err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
console.log(filename + ' : ' + values)
aspect = (values.width / values.height)
widths.forEach(function (width, widthIndex) {
height = Math.round(width / aspect)
console.log('resizing ' + filename + 'to ' + height + 'x' + height)
this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
if (err) console.log('Error writing file: ' + err)
})
}.bind(this))
}
})
})
}
})
Asynchronous JavaScript, or JavaScript that uses callbacks, is hard to get right intuitively
//Non-blocking, asynchronous way
fs.readFile('./txt/start.txt', 'utf-8', (err, data1) => {
if (err) return console.log('ERROR! 💥');
fs.readFile(`./txt/${data1}.txt`, 'utf-8', (err, data2) => {
console.log(data2);
fs.readFile('./txt/append.txt', 'utf-8', (err, data3) => {
console.log(data3);
fs.writeFile('./txt/final.txt', `${data2}\n${data3}`, 'utf-8', err => {
console.log('Your file has been written 😁');
})
});
});
});
console.log('Will read file!');
👼🏼 SOLUTION ??? 👼🏼
deck
By rodolphe
deck
- 435