by Rodolphe Duterval
Rodolphe Duterval
32 🐳
Dev Web FullStack
Copiage bête non toléré
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
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
go on : http://nodejs.org & install it !
node -v
v13.8.0
mkdir node-farm
//index.js
const hello = 'Hello ESTIAM';
console.log(hello)
// lancer dans votre terminal
// node index.js
//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
//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
blocking 👎
non-blocking 🤟
vs.
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!');