전체 글

전체 글

    [Nodejs] 미들웨어의 사용과 자주 쓰이는 미들웨어

    ✅ 미들웨어(middleware) request와 response 사이에 위치하여 미들웨어(middleware)라고 불린다 ✅ 미들웨어의 사용 예시 app.use(미들웨어) 모든 경로 요청에서 미들웨어 사용 app.use('/abc', 미들웨어) abc로 시작하는 모든 요청에서 미들웨어 사용 app.post('/abc', 미들웨어) abc로 시작하는 POST 요청에서 미들웨어 사용 app.use 나 app.get 같은 라우터에 미들웨어를 여러 개 장착 가능하다 라우터 끝단에서 반드시 next 함수를 호출해야 다음 미들웨어를 실행할 수 있다 (보통 미들웨어는 내부적으로 next 함수를 호출하기에 따로 적어줄 필요는 없다) 미들웨어는 동시에 여러개를 장착할 수도 있다 ✅ 실무에서 자주 사용하는 패키지 ✔ m..

    [Nodejs] express 서버 열기

    ✅ package.json 초기화 및 express 모듈 설치 npm init ... npm install express ✅ express import 및 express 객체 생성 const express = require('express'); const app = express(); ✅ app.set 과 app.get app.set(키, 값)으로 데이터 저장 app.get(키)로 데이터 가져올 수 있음 ✅ GET 메소드 라우팅 app.get('/', (req, res)=>{ res.send('Welcome!'); }); ✅ 서버 실행 app.listen(app.get('port'), ()=>{ console.log(`Server online on port ${app.get('port')}`); }); ..

    [Nodejs] 쿠키와 세션

    ✅ 쿠키 클라이언트의 정보를 기억하기 위해 서버는 요청에 대한 응답으로 쿠키를 같이 보내고 클라이언트의 웹 브라우저는 다음 요청에 쿠키를 자동으로 같이 보내어 서버가 쿠키를 읽고 클라이언트가 누구인지 파악한다 쿠키는 request의 헤더(Cookie)에 담겨 서버에 전송된다 브라우저는 response의 header (Set-Cookie)를 읽어서 저장한다 const http = require('http'); const PORT = 8080; http.createServer(async (req, res) =>{ try{ console.log(req.url, req.headers.cookie); res.writeHead(200, {'Set-Cookie' : 'name=gengminy'}); return re..

    [Nodejs] http 모듈로 간단한 REST API 사용

    ✅ http 모듈 불러오기 const http = require('http'); ✅ http 서버 생성 const http = require('http'); const PORT = 8080; http.createServer(async (req, res) =>{ res.writeHead(200, {'Content-Type' : 'text/html; charset=utf-8'}); return res.end("hello my server"); } ).listen(PORT, ()=>{ console.log(`server online on port ${PORT}!`); }); header 에 http status를 같이 담아서 전송 💻console 💻client - localhost:8080 ✅ REST API..

    시작

    2022-01-17 기술블로그 합니다 아무도 못말려(짱구 아님)