logo

JEONGGON

    블로그
github
mode
목 차
down-arrow

Node.js index.js 파일

2024.03.13.

post-thumbnail

index.js 파일

1. index.js 파일이란?

  • Node.js를 만든 개발자 “Ryan Dahl”이 Node.js에서 후회하는 요소 중 하나로 index.js를 꼽았으며, 이유는 index.js모듈의 로딩 시스템을 복잡하게 만들기 때문이라고 함

  • 모든 소스 모듈들을 lib 폴더에 index.js 파일과 함께 넣어주고 소스 모듈들을 내보내기하여 index.js 파일에 모두 불러와 하나로 결합한 뒤, 사용

  • 즉, index.js 파일은 여러 모듈을 하나로 합치는 역할을 함


1-1. index.js 파일 사용

  • 로딩 시스템이 복잡해지기에 사용 지양
// lib/index.js

module.exports = {
  request: require('./request'),
  response: require('./response')
}
// https.js (다른 모듈)

const lib = require('./lib');

function makeRequest(url, data) {
  lib.request.send();
  return lib.response.read();
}

1-2. 지향하는 방법

  • index.js 파일을 경유하지 않고 바로 불러와 사용하기
// https.js

const { send } = require('./lib/request');
const { read } = require('./lib/response');

function makeRequest(url, data) {
  send();
  return read();
}
nodejsprogrammingjavascriptfileindex
profile

조정곤

주니어 프론트엔드 개발자

github
linkedin
instagram
email

< 이전글

Node.js 웹 서버 생성

다음글 >

Node.js 모듈

Nodejs 포스트 (8)

down-arrow
Node.js 소개Node.js Blocking, Non-blockingNode.js Event EmitterNode.js 프로세스, 스레드Node.js 모듈Node.js 패키지Node.js index.js 파일Node.js 웹 서버 생성