Javascript 49

[ Node.js ] - type module에서 require 사용하기

require / exports 와 import / export commonjs => require / exports es6 module => import (import * as name from 'name')/ export / export default 특정 파일이나 모듈을 불러오거나 내보내기 할 때 사용할 수 있는 것은 require/exports 또는 import/export 이다. 기본적으로 npm init -y을 통해 프로젝트가 생성된 경우 기본적인 설정값은 require/exports로 되어있다. import/export는 es6에서 발생한 것으로써 package.json파일에서 "type":"module"을 설정해줌으로써 사용할 수 있다. (참고로 require/exports는 "type":..

Javascript/Node.js 2022.08.17

[ Node.js ] - lodash 사용하기

lodash lodash는 자바스크립트 내에서 배열, 문자열, 객체, 숫자 등의 작업을 더욱 손쉽게 할 수 있도록 해주는 라이브러리이다. 사용법 > npm i lodash import { createRequire } from 'module'; const require = createRequire(import.meta.url); const _ = require('lodash'); // lodash를 require()를 통해 가져와 사용할 수 있다. 예시 _.assignIn() _.assignIn({}, [리소스])와 같은 형식으로 사용하며, 인자값으로 준 것들을 새로운 객체로 생성하여 할 때 이용된다. import { createRequire } from "module"; const require = cr..

Javascript/Node.js 2022.08.15

유용한 함수 padStart/padEnd/flat

padStart / padEnd string.padStart( [인자1], [인자2] ) 형태로 사용된다. 인자1로 주어진 수만큼의 여백을 앞에서부터 인자2로 채운다. const phoneNumber = "12345678"; console.log(phoneNumber.padStart(11, "010")); //01012345678 string.padEnd( [인자1], [인자2] ) 형태로 사용된다. 인자1로 주어진 수만큼의 여백을 뒤에서부터 인자2로 채운다. const height = "153"; console.log(height.padEnd(5, "cm")); // 153cm flat 2차원 배열을 1차원으로 만들어 준다. const arr = [ [1, 2], [3, 4], [5, 6], ]; co..

Javascript/etc 2022.08.15

es6 문법 알아보기

Default Parameters 함수에 기본값을 줄 수 있다. 함수에 인자값을 주면 해당 인자를 사용하고, 인자값을 넣지 않으면 기본값을 사용하게 된다. const makeLatte = (shot = 1) => { return `milk1 + shot${shot}`; }; const adult = makeLatte(); console.log(adult); // milk1 + shot1 const child = makeLatte(0); console.log(child); // milk1 + shot0 const programmer = makeLatte(2); console.log(programmer); // milk1 + shot2 Conditional Operator (삼항 연산자) 문법은 [ 조건식 ..

Javascript/etc 2022.08.15

구조 분해 할당 (Destructuring assignment)

구조 분해 할당 : Destructuring assignment 배열이나 객체의 속성을 쉽게 해체하여 변수에 담아주는 javascript 표현식이다. 구조 분해 할당은 크게 배열 구조 분해와 객체 구조 분해로 나뉘게 된다. - 배열 구조 분해 배열에 담긴 요소들을 순서대로 해당 변수에 담아준다. const numbers = ["one", "two", "three"]; const [a, b, c] = numbers; console.log(`one: ${a} | two: ${b} | three: ${c}`); // one: one | two: two | three: three 각각의 변수에 배열을 통해 값을 할당하여 줄 수 있다. let a, b; [a, b] = [1, 2]; console.log(`a: ..

Javascript/etc 2022.08.15

[ Node.js ] - winson이용하여 로그 남기기

winston 로그 관리를 위해 사용되는 모듈의 일종이다. 사용법 > npm i winston winston-daily-rotate-file - winston이 실질적인 로그를 남기는 모듈이고, - winston-daily-rotate-file 모듈을 로그를 일자별로 파일로 관리할 수 있도록 해주는 모듈이다. 기본 틀 import winston from "winston"; import wistonDaily from "winston-daily-rotate-file"; const { combine, printf, label, timestamp } = winston.format; const logFormat = printf((level, message, label, timestamp) => { return `..

Javascript/Node.js 2022.08.14

[ Node.js ] - compression으로 데이터 압축하기

Compression 많은 양의 데이터가 네트워크 전송시 네트워크에 부담을 줄 수 있는데, compression은 서버에서 데이터를 압축하여 준다. 이를 통해 서버는 클라이언트로 데이터를 전송할 때, 압축된 데이터를 보냄으로써 네트워크 전송 속도를 높이고, 비용을 줄일 수 있다. 사용법 > npm i --save compression compression을 이용하여 데이터 압축하기 전 import express from "express"; const app = express(); app.get("/", (req, res) => { const payload = "dkssdjssdjfkalsjfklasjklfjlkakssdjfkalsjfklasjklfjlkasjfsjfkalsjfklasjklfjlkakssd..

Javascript/Node.js 2022.08.14

vercel로 프론트 배포하기

vercel? https://cocobi.tistory.com/105 [Next.js] Vercel로 프론트 배포하기 ❔ Vercel 이란 Vercel 은 Next.js 에서 제공하는 배포플랫폼으로 [빌드 + 배포 + 호스팅] 서비스를 제공한다. Next.js 공식문서에서는 Vercel를 통한 Front Project 배포를 권장하고 있으며, github의 레파지토.. cocobi.tistory.com Vercel 은 Next.js 에서 제공하는 배포플랫폼으로 [빌드 + 배포 + 호스팅] 서비스를 제공해준다. 1. github repository 생성 2. 프로젝트 생성 > npx create-react-app my-app --template typescrip 위의 명령어로 설치안되는 경우 먼저 yarn..

Javascript/etc 2022.07.27

[ Node.js ] - node-cron 활용하여 스케줄러 작동시키기

node-cron https://www.npmjs.com/package/node-cron node-cron A simple cron-like task scheduler for Node.js. Latest version: 3.0.1, last published: 2 months ago. Start using node-cron in your project by running `npm i node-cron`. There are 753 other projects in the npm registry using node-cron. www.npmjs.com node-cron은 일정한 시간 간격으로 특정한 작업을 실행할 수 있도록 해주는 모듈이다. > npm i node-cron > npm i --save-dev @t..

Javascript/Node.js 2022.07.27