Node.js Express 맛보기

강재민·2023년 9월 4일
post-thumbnail

크롬의 JS엔진 V8을 떼어놓은 event-driven, non-blocking I/O 의 특성을 가진 실행환경 블라블라

express 설치하기

//node.js 설치 후
npm init

entry point: (index.js) 원하는 파일명

npm install express

서버를 띄우기 위한 기본세팅

const express = require('express');
const app = express();

app.listen(포트번호, 실행할 코드);

로컬서버와 연동하기

//터미널
node 파일명.js

이렇게 수동으로 할 수도 있지만, 변경사항이 생길 때마다 crtl+c 로 종료 후 다시 선언 해주어야 한다. 그래서 nodemon 으로 자동화 해줄것이다.

npm install -g nodemon

보안오류 생길 시
관리자 권한으로 PowerShell 실행해주고

    1. executionpolicy 를 입력했을 때 restricted가 나온다면 문제가 발생한다.
    1. set-executionpolicy unrestricted 를 입력해주고 y를 선택해준다.

get 요청 처리하기

app.get('/경로', function (요청, 응답) {
  응답.send('get요청 하셨네유');
  응답.sendFile(__dirname + '/파일경로 예) index.html sub.html')
});

post 요청 처리하기

app.use(express.urlencoded({extended: true})) //post로 요청한 값 url 인코딩으로 알아서 해석해서 가져오기

app.post('/경로', function (요청, 응답) {
  응답.send('전송완료');
  console.log(요청.body);
});
profile
말많은 개발자의 111강

0개의 댓글