Node + React Basic [1]

YulHee Kim·2021년 1월 5일

node

목록 보기
1/4
post-thumbnail

💫 Node + React 기초 강의 💫

💡 Node.js란?

NodeJS 는 자바스크립트 엔진에 기반해 만들어진 서버 사이드 플랫폼입니다.

비동기 이벤트 주도 JavaScript 런타임으로써 Node.js 는 확장성 있는 네트워크 애플리케이션을 만들 수 있도록 설계되었습니다. 다음 "hello world" 예제는 다수의 연결을 동시에 처리할 수 있습니다. 각 연결에서 콜백이 실행되는데 실행할 작업이 없다면 Node.js 는 대기합니다.
:: 출처 (https://nodejs.org/ko/about/)

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

1. node, express다운, express를 통한 간단한 앱 구동

1. 노드가 이미 다운되어 있는 지 확인

node -v

2. npm init

3. index.js 파일 생성 후 package.json 수정

 "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

"start"스크립트가 시작점 -> npm run start 다음에 index.js가 실행

4. express js 다운 받기

npm install express --save

💡 express js 란?

Express.js는 Node.js의 핵심 모듈인 http와 Connect 컴포넌트를 기반으로 하는 웹 프레임워크다. 그러한 컴포넌트를 미들웨어(middleware)라고 하며, 설정보다는 관례(convention over configuration)와 같은 프레임워크의 철학을 지탱하는 주춧돌에 해당한다. 즉, 개발자들은 특정 프로젝트에 필요한 라이브러리를 어떤 것이든 자유롭게 선택할 수 있으며, 이는 개발자들에게 유연함과 수준 높은 맞춤식 구성을 보장한다.

  • HTTP 요청 본문 파싱
  • 쿠키 파싱
  • 세션 관리
  • URL 경로와 HTTP 요청 메서드를 기반으로 한 복잡한 if 조건을 통해 라우팅을 구성
  • 데이터 타입을 토대로 한 적절한 응답 헤더 결정
    :: 출처 (https://wikibook.co.kr/article/what-is-expressjs/)

5. node_module

다운 받은 dependencies들은 다 이 폴더에서 관리됨

6. index.js에서 기본적인 express js 앱 만들기

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

:: (http://expressjs.com/en/starter/hello-world.html)

✉️ 다음 글에선 Mongo DB와 연결을 다뤄보도록 하겠습니당
Node는 센서를 구동할 때 잠깐 다뤄본 적이있는데 웹프레임워크로 이용하려니 몰랐던 부분이 많아서 더 강의가 재밌게 느껴지네용🧚

profile
백엔드 개발자

2개의 댓글

comment-user-thumbnail
2021년 1월 5일

팬씨하네요

1개의 답글