ES6 module in nodejs

 https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules/53826979#53826979

Deno is here !

 https://deno.land/

API for test

 https://dev.to/api/articles

Permet de charger une série d'articles !

module fs

 const fs = require("fs").promises;

const path = require("path");

async function findFiles(folderName) {
  const items = await fs.readdir(folderName, { withFileTypes: true });
  items.forEach((item) => {
    if (path.extname(item.name) === ".json") {
      console.log(`Found file: ${item.name} in folder: ${folderName}`);
    } else {
      findFiles(path.join(folderName, item.name));
    }
  });
}
findFiles("stores");

Considérons la structure : 


$ node index.js 
Found file: sales.json in folder: stores\201
Found file: sales.json in folder: stores\203
Found file: sales.json in folder: stores\201\204
Found file: sales.json in folder: stores\201\204\202

fetch/ asyn

Promise

  1. function getFromGitHub() {
  2.   const userName = 'dupontdenis';
  3.   const url = 'https://api.github.com/users';

  4.   fetch(`${url}/${userName}/repos`)
  5.     .then(reposResponse => {
  6.       return reposResponse.json();
  7.     })
  8.     .then(userRepos => {
  9.       console.log(userRepos[0].owner.avatar_url)
  10.       document.body.insertAdjacentHTML('afterbegin',`<img src=${userRepos[0].owner.avatar_url}>`)
  11.     })
  12.     .catch(err => {
  13.       console.log(err);
  14.     });
  15. }

  16. getFromGitHub();

Avec sync


  1. async function getFromGitHub() {
  2.   try {
  3.     const userName = 'dupontdenis';
  4.     const url = 'https://api.github.com/users';
  5.     const reposResponse = await fetch(`${url}/${userName}/repos`);
  6.     const userRepos = await reposResponse.json();

  7.           document.body.insertAdjacentHTML('afterbegin',`<img src=${userRepos[0].owner.avatar_url}>`)

  8.   } catch (error) {
  9.     console.log(error);
  10.   }
  11. }


  12. getFromGitHub();

DIFF entre npm yarn

 https://classic.yarnpkg.com/en/docs/migrating-from-npm#toc-cli-commands-comparison