TIL027_210422

JIYOON·2021년 4월 24일
0

TIL

목록 보기
27/42
post-thumbnail

🍊 감상

오랜만의 본가 이동으로 며칠 동안은 가족과 시간을 보낼 예정

📙 열품타 코딩 시간 5hour
👍🏼 -
👎🏼 -

🚀 목표

  • Udemy에서 Javascript 강좌 수강하기 (365/682)
  • 개인 프로젝트 진행

📣 The Web Developer Bootcamp 2021

31. First brush with Node

318. Process & Argv

process is an object that's available

process.version
process.release
process.cwd() -> path to where I am currently working or where I'm currently running node. -> useful when I want to make a file or a folder relative to this path

touch args.js
node args.js

Node documentation

node args.js puppies chickens hello
// /user/local/bin/node (executable path)
// /Users/colt/Code/args.js (path to the file where you are actually running)
// puppies
// chickens
// hello 

node greeter.js colt rusty tammy //터미널 입력

const args = process.argv.slice(2); //js 파일
//slice 쓰는 이유: process.argv 리스트의 첫번째 두개는 항상 path들이기 때문, 커맨드 라인의 인자에만 접근하기 위해
for (let arg of args) {
  console.log(`hi ${arg}`}
}

319. file system module crash course

fs is not a thing by default

const fs = require('fs'); (this is already in node, but not in scope, so we have to go tell node we want to use it)

fs.mkdir('Dogs', { recursive : true }, (err) => {
  console.log("In the callback")
  if (err) throw err;
});
console.log("i come from mkdir in the file")
//i come from mkdir in the file
//In the callback //asynchronous

fs.mkdirSync('cats');
console.log('i come after mkdir in the file');
//synchronous
node boilerplate.js Project1

process.argv[2] // first one is executable path, second one is path to the file, third one at index of 2 in argv is the first argument passed in.

const fs = require('fs');
const folderName = process.arg[2] || 'Project'
fs.mkdirSync(folderName);
//empty Project1 폴더를 만들어 준다.

node boilerplate.js
//Project 폴더를 만들어 준다.

fs.writeFile

write data to a file replacing file if it already exists -> create file if it does not exist

const fs = require('fs');
const folderName = process.arg[2] || 'Project'

try {
  fs.writeFileSync(`${folderName}/index.html`)
  fs.writeFileSync(`${folderName}/app.js`)
  fs.writeFileSync(`${folderName}/style.css`)
} catch (e) {
  console.log('something went wrong')
  console.log(e);
}

node boilerplate.js Portfolio
ls
cd Portfolio //index.html app.js style.css

32. Exploring Modules & The NPM Universe

321. Working with module.exports

require code from other files (reuse in different files)

//in math.js file

const add = (x, y) => x+y;
const PI = 3.14159;
const square = x => x*x;

//no.1
module.exports.add = add;
module.exports.PI = PI;
module.exports.square = square;

//no.2
const math = {
  add:add,
  PI:PI,
  square:square
}
module.exports = math;

//no.3
module.exports.add = (x, y) => x+y;
module.exports.PI = 3.14159;
module.exports.square = x => x*x;

//no.4 shortcut syntax
exports.add = add;
exports.PI = PI;
exports.square = square;

//in app.js file

//no.1
const math = require('./math'); //./is referencing something in this directory
// . means this directory 헐 그래서 ..이 상위 디렉토리를 가리키는 것...
console.log(math) //{} -> exports 안 하면 empty object가 나온다
console.log(math.squre(9)) // 81

//no.2 destructure
const {PI, square} = require('./math');
console.log(PI)

322. Requiring a directory

mkdir shelter
cd shelter
touch blue.js sadie.js janet.js 

//in janet.js
module.exports = {
  name: 'janet',
  color: 'orange'
}
//이런식으로 세 개 파일 다 오브젝트 만든다
//같은 파일 내에 index.js 파일을 만든다

const blue =require('./blue')
const blue =require('./sadie')
const blue =require('./janet')
//파일 모두 import한 후 allCats라는 single array에 저장
const allCats = [blue,sadie,janet]
console.log(allCats);
module.exports = allCats;

//index.js 파일은 node에서 main file, entry pile 역할을 한다
//파일 내부를 전부 찾아볼 때 node는 index.js파일을 볼 것 -> 이 파일에서 export하는 게 폴더에서 export하는 것
//require shelter 할 때는 index.js를 불러오는 것

//폴더 외부의 app.js 파일
const cats = require('./shelter')
console.log(cats)
// [{ name: 'janet', color: 'orange'}, 등 세 개]

323. Introducing NPM

NPM is really two things:
1. A library of thousands of packages published by other developers that we can use for free
2. A command line tool to easily install and manage those packages in our Node projects.

package is code somebody else has written. NPM is standardised repository for those node packages, stands for node package manager and node itself comes with a NPM command line tool that we will use to easily install and manage those packages.

323. Installing packages

npm install

const jokes = require("give-me-a-joke")
//패키지는 파일 경로를 입력하지 않고 이름만 입력해도 된다
console.dir(jokes)
//패키지에 포함된 메소드를 볼 수 있다

const jokes = require('give-me-a-joke');
const colors = require('colors');
jokes.getRandomeDadJokes(function (joke) {
  console.log(joke.rainbow);
});

0개의 댓글