Wednesday, August 17, 2022

Commands, features and Node.js usage

https://nodejs.org/en/docs/

https://docs.npmjs.com/

npm i nodemon -g


https://expressjs.com/en/starter/installing.html

npm list

npm list -g

npm i package@1.0.0 // install npm package v 1.0.0

npm uninstall package

npm outdated

node -v

>

press Tab key // to see global variables

> 2+3

5

> _-5 // _ => previous result

0

> String. and press Tab key to see list of methods


Reading text file:

const fs = require('fs'); // file system module https://nodejs.org/dist/latest-v18.x/docs/api/fs.html

const textIn = fs.readFileSync('./txt/input.txt', 'utf-8'); // DO NOT USE sync in callback functions

Write into text file:

const textOut = `This is test: ${textIn}.\nCreated on ${Date.now()}`;

fs.writeFileSync('./txt/output.txt', textOut);

console.log('File written');


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


Async/Await to escape "Callback Hell".

() => arrow function DO NO GET own this, but from parent function


GET POST PUT PATCH DELETE OPTIONS

GET => HTTP request: start line, headers, body

No comments:

Post a Comment