본문 바로가기

Programming/Nodejs tips

(3)
Nodejs 비밀번호 암호화 요즘에는 소셜 로그인을 많이 하기 때문에 비밀빈호가 전혀 필요하지 않는 경우가 많다. 비밀번호가 필요한 경우, 일반적으로 본인 외 누구도 알아서는 안되는 코드이기 때문에 암호화가 기본이다. 이번에는 가장 많이 쓰이는 단방향 암호화를 통해 비밀번호를 아주 비밀스럽게 만드는 방법을 코드로 알아보자. import { pbkdf2Sync, randomBytes } from 'node:crypto'; const encryptPassword = (password) => { const salt = randomBytes(64).toString('base64'); // random salt 생성 const iteration = 100000; // 해시 함수 반복 수 const keylen = 64; // 암호화 된 비밀..
Nodejs + Express + Typescript 프로젝트 세팅 필요 선행 작업 $ npm i -g yarn $ npm i -g typescript *** 만약 git 이 설치되어 있지 않다면, 아래 명령어 실행 $ brew install git 프로젝트 생성 프로젝트를 시작할 폴더를 생성 후 해당 폴더 내에서 아래 명령어 실행 $ yarn init -y $ tsc --init $ git init 라이브러리 추가 Express, nodemon, typescript 설치 $ yarn add express $ yarn add -D @types/express @types/node ts-node typescript nodemon tsconfig.json 설정 tsc --init 명령으로 파일이 생성되었을 것이다. 해당 config 에 typescript 컴파일 옵션을 추가한..
Nodejs 로 텍스트 파일 가져오기 node v18 텍스트 파일을 읽기 위해 nodejs 에서는 readFile, readFileSync 가 있다. 1. async/await readFile import { readFile } from 'node:fs/promises'; const path = 'YOUR_TEXT_FILE_PATH'; try { const text = await readFile(path, 'utf8'); console.log(text); } catch (err) { console.log(err); } 2. Callback readFile import { readFile } from 'node:fs'; const path = 'YOUR_TEXT_FILE_PATH'; readFile(path, 'utf8', (err, data..