꾸준한프로그래밍

패스트캠퍼스 챌린지 5일차 - Node JS 핵심 개념 정리 본문

백엔드

패스트캠퍼스 챌린지 5일차 - Node JS 핵심 개념 정리

꾸준이 2021. 9. 10. 21:31

0. 공부 기록

공부 시간 : 19 : 56 ~ 21 : 29

1. 모듈 레졸루션과 관련해서 (모듈을 임포트하거나, 출력할 때)

module.exports & require 의 특징

// animals.js
const animals = ['dog', 'cat']

module.exports = animals

//main.js
console.log(require('./animals'))
console.log(module.require('./animals'))
// ['dog', 'cat']

require 의 특징 (https://nodejs.org/en/knowledge/getting-started/what-is-require/)

-> Node 에서는 파일하나하나가 모듈인 셈

 

 

그리고 모듈을 입/출력하는 방법이 다름.

Common JS : require

ECMAScript : export, import

-> 두 방식을 혼합해서 사용할 수 있음

 

export import 의 특징

// animals.js
const animals = ['dog', 'cat']

export default = animals

//main.mjs
import animals from './animals.js'
console.log(animals)
// ['dog', 'cat']

궁금한 점 : React에서는 ECMAScript 를 사용하는데, 확장자가 mjs가 아닌 js일까

 

require를 통해서 여러번 임포트를 해도 같은 객체임.
그리고 실행도 한번만 함.

일반적으로 require의 모듈은 상대경로 지정을 통해서 가져오지만,
node standard library 및 node_modules 폴더 아래에 있는 모듈은 절대경로를 통해 가져온다.

 

2. 패키지 매니저에 대해서

https://www.npmjs.com/

 

npm

Bring the best of open source to you, your team, and your company Relied upon by more than 11 million developers worldwide, npm is committed to making JavaScript development elegant, productive, and safe. The free npm Registry has become the center of Java

www.npmjs.com

npm install 시에 package.json(대충의 버전 정보) / package-lock.json(패키지 모듈의 정확한 버전 정보)과 node_modules가 생성됨.

 

 

semantic Versioning 으로 버전을 표시함 -> major.minor.patch

npm install express@1.2.3 -> 해당 버전을 설치할 수 있음

 

3. lint 를 통해서 에러 찾기

./node_modules/.bin/eslint src/**/*

-> src 아래의 모든 파일을 검사해라 

또한 package.js 안의 script에 명령어를 작성하면, node_modules/.bin 폴더를 우선 검색 함. (사진 처럼 줄일 수 있음)

 

4. Node JS 에는 어떤 내장 객체들이 있나

->파일과 관련된 내장 객체

-> 파일 입출력과 관련된 객체 (process, 스트림 에 대해서 )

 

-> setTime ~

let count = 0
const handle = setInterval( () => {
	console.log("done!")
    count += 1
    
    if(count === 4){
    	console.log("done")
        clearInterval(handle)
   	}
}, 1000)

 

*process.argv -> 명령어의 인자 값

 

5. 스탠다드 라이브러리

하기 링크에 들어가면 더 자세한 내용을 살펴볼 수 있음

https://nodejs.org/dist/latest-v14.x/docs/api/

 

Index | Node.js v14.17.6 Documentation

 

nodejs.org

OS

// os 관련 정보
const os = require("os")

console.log(
	['arch', os.arch()],
    ['platform', os.platform()],
    ['cpus', os.cpus()],
)

Child Process

-> 비디오 처리를 따로 한다던지 등

-> 노드내에서 구현되어 있지 않은 것들을 직접 구현할 때 사용하면 좋음.

 

파일 시스템 관련 Path

const path = require("path")
const fs = require("fs")

const filePath = path.resolve(__dirname, './test.txt')
console.log('filePath', filePath)

-> 절대경로 + 파일 이름

 

 

 

환급 챌린지 (5/30)

 

 

본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.

https://bit.ly/37BpXiC

 

패스트캠퍼스 [직장인 실무교육]

프로그래밍, 영상편집, UX/UI, 마케팅, 데이터 분석, 엑셀강의, The RED, 국비지원, 기업교육, 서비스 제공.

fastcampus.co.kr

#패스트캠퍼스 #패캠챌린지 #직장인인강 #직장인자기계발 #패스트캠퍼스후기 #한번에끝내는Node.js웹프로그래밍초격차패키지