Node JS
Node : 브라우저 밖에서 실행되는 JavaScript
Node JS 개요
자바스크립의 런타임
브라우저가 없으면 문서 객체 모델을 쓸 수 없고 사용자의 입력, 이벤트 등을 사용할 수 없다.
Node는 어디에 사용하는가
- Web Servers (웹서버구축, 즉 서버측 로직을 써서 풀스택 어플리케이션 만듦)
- Command Line Tools(전통적 사용자 인터페이스가 없는 경우 명령줄에서 실행되는 애플리케이션)
- Native Apps(VSCode is a Node app!)
- Video Games
- Drone Software
- A Whole Lot More
Node의 인기있는 프레임워크 : Express
Node 설치
1. 우선 node가 설치되어있는지 확인
Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
nodejs.org
다운받아준다.
3.
터미널창에 node확인
설치 오류 메세지뜨면 새 터미널 창에서 확인
Node REPL
브라우저의 자바스크립트 콘솔은 REPL이다.
코드를 콘솔에 입력하면 코드를 읽고 평가하고 뭔가를 출력.
REPL은 HTML에 파일 스크립트가 있고 브라우저에서 스크립트를 열고 실행한다.
REPL은 사용자가 뭔가를 계속 입력하기를 기다린다.
사용자가 입력하면 그때 평가한다.
크롬의 자바스크립트 콘솔과 REPL의 개념은 같다.
Node 파일실행
for(let i=0; i<10;i++){
console.log("HELLO!")
}
노드에서 파일실행 했음.
프로세스와 Argv
현재 노드의 버전과 현재 내가쓰고 있는 버전을 확인 가능하다
process 프로세스는 노드에서 쓰는 객체이다. 전역범위에 있다.
https://nodejs.org/docs/latest-v18.x/api/process.html
Process | Node.js v18.16.0 Documentation
Process# Source Code: lib/process.js The process object provides information about, and control over, the current Node.js process. import process from 'node:process';const process = require('node:process'); Process events# The process object is an instance
nodejs.org
프로세스
const args = process.argv.slice(2);
for(let arg of args){
console.log(`Hi there, ${arg}`)
}
파일시스템 모듈의 충돌 과정
FS (File System) 파일 시스템에 대한 모듈
파일 생성, 읽고, 추가, 새로운 파일 생성
새로운 디렉토리생성, 파일을 열고 닫는 메서드
https://nodejs.org/docs/latest-v18.x/api/fs.html
File system | Node.js v18.16.0 Documentation
nodejs.org
비동기
const fs = require('fs');
console.log(fs)
mkdir('DOg', { recursive: true }, (err) => {
console.log("in the callback")
if (err) throw err;
});
console.log("I come after mkdir")
동기
const fs = require('fs');
console.log(fs)
fs.mkdirSync('Cats');
console.log("I come after mkdir")