[TIL] Day37 #Express

Beanxxยท2022๋…„ 6์›” 17์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
37/120
post-thumbnail

2022.06.17(Fri)

[TIL] Day37
[SEB FE] Day37

โ˜‘๏ธย Express

Node.js ํ™˜๊ฒฝ์—์„œ ์›น ์„œ๋ฒ„ ๋˜๋Š” API ์„œ๋ฒ„ ์ œ์ž‘์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์ธ๊ธฐ ํ”„๋ ˆ์ž„์›Œํฌ

1. ๋ฏธ๋“ค์›จ์–ด ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
2. Router ์ œ๊ณต

// express install
npm install express
const express = require('express')
const app = express();
const port = 3000; 

// ๋ฃจํŠธ URL(/)์— ๋Œ€ํ•œ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต: 'Hello World!'
// ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฒฝ๋กœ๋Š” 404 Not Found๋กœ ์‘๋‹ต
app.get('/', (req, res) => {
	res.send('Hello World!');
})

app.listen(port, () => {
	console.log(`Example app listening on port ${port}`)

๐Ÿ“Žย Routing

๋ฉ”์„œ๋“œ์™€ URL๋กœ ๋ถ„๊ธฐ์ ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ
โ‡’ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ํ•ด๋‹นํ•˜๋Š” Endpoint์— ๋”ฐ๋ผ ์„œ๋ฒ„๊ฐ€ ์‘๋‹ตํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ฒฐ์ •

const router = express.Router();

router.get('/lower', (req, res) => {
	res.send(data);
})

router.post('/lower', (req, res) => {
	~~~
})

๐Ÿ“Žย Middleware

ํ”„๋กœ์„ธ์Šค ์ค‘๊ฐ„์— ๊ด€์—ฌํ•˜์—ฌ ํŠน์ • ์—ญํ•  ์ˆ˜ํ–‰

โžฐย ๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ ์ƒํ™ฉ

  1. POST ์š”์ฒญ ๋“ฑ์— ํฌํ•จ๋œ body(payload)๋ฅผ ๊ตฌ์กฐํ™”ํ•  ๋•Œ

    // Node.js
    
    let body = [];
    request.on('data', (chunk) => {
    	body.push(chunk);
    }).on('end', () => {
    	// body ๋ณ€์ˆ˜์—” ๋ฌธ์ž์—ด ํ˜•ํƒœ์˜ payload(body)๊ฐ€ ๋‹ด๊ฒจ์ ธ ์žˆ์Œ
    	body = Buffer.concat(body).toString();
    });
    // body-parse middleware๋กœ ์œ„์˜ ๊ณผ์ •์„ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ ๊ฐ€๋Šฅ
    npm install body-parser
    
    const bodyParser = require('body-parser');
    const jsonParser = bodyParser.json();
    
    app.post('/users', jsonParser, function (req, res) {
    	~~~
    })
    // Express v4.16.0๋ถ€ํ„ด ๋”ฐ๋กœ body-parser ์„ค์น˜ x
    // Express ๋‚ด์žฅ middleware์ธ express.json() ์‚ฌ์šฉ
    const jsonParser = express.json();
    
    app.post('/users', jsonParser, function (req, res) {
    	~~~
    })
  1. ๋ชจ๋“  ์š”์ฒญ/์‘๋‹ต์— CORS ํ—ค๋”๋ฅผ ๋ถ™์—ฌ์•ผ ํ•  ๋•Œ

    // Node.js
    
    const defaultCorsHeader = {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type, Accept',
      'Access-Control-Max-Age': 10
    };
    
    if (req.method === 'OPTIONS') {
      res.writeHead(201, defaultCorsHeader);
      res.end()
    }
    // cors middleware๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์˜ ๊ณผ์ •์„ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„
    npm install cors
    
    const cors = require('cors');
    
    // ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•œ CORS ํ—ˆ์šฉ
    app.use(cors());
    
    // ํŠน์ • ์š”์ฒญ์— ๋Œ€ํ•œ CORS ํ—ˆ์šฉ
    app.get('/products/:id', cors(), function (req, res, next) {
    	res.json({msg: 'This is CORS'})
    })
  1. ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด URL์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ™•์ธํ•  ๋•Œ

    const express = require('express');
    const app = express();
    
    const myLogger = function (req, res, next) {
    	next();
    };
    
    // function(): middleware function
    // req: ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜์— ๋Œ€ํ•œ HTTP ์š”์ฒญ ์ธ์ˆ˜
    // res: ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜์— ๋Œ€ํ•œ HTTP ์‘๋‹ต ์ธ์ˆ˜
    // next: ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ฝœ๋ฐฑ ์ธ์ˆ˜ => ๋‹ค์Œ ๋ฏธ๋“ค์›จ์–ด ์‹คํ–‰ ์—ญํ• 
    
    app.use(myLogger);
    
    app.get('/', function (req, res) {
    	res.send('Hello World');
    });
    
    app.listen(3000);
  2. ์š”์ฒญ ํ—ค๋”์— ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋•Œ

  • ํ† ํฐ(Token): ์ฃผ๋กœ ์‚ฌ์šฉ์ž ์ธ์ฆ์— ์‚ฌ์šฉ
    app.use((req, res, next) => {
    	if(req.headers.token) { // ํ† ํฐ์ด ์žˆ๋‹ค๋ฉด
    		req.isLoggedIn = true;
    		next();
    	} else { // ํ† ํฐ์ด ์—†๋‹ค๋ฉด error ์ฒ˜๋ฆฌ
    		res.status(400).send('invalid user');
    	}
    })


โ˜‘๏ธย [Pair] Refactoring Mini-Node-Server

์–ด์ œ Node.js๋กœ๋งŒ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ ๊ธธ์ด๋Š” ๊ฝค ๊ธธ์—ˆ์—ˆ๋Š”๋ฐ ํ™•์‹คํžˆ Express๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ๊นŒ ๊น”๊ผผํ•˜๊ฒŒ ๋ช‡ ์ค„๋งŒ์— ๋ฆฌํŒฉํ† ๋ง ๊ตฌํ˜„ ๋- ๐Ÿ‘

// express๋กœ refactoring
const express = require("express");
const http = require("http");
const app = express();
const cors = require("cors");
// ์›์‹œ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๋„ parsing ํ•ด์ฃผ๋„๋ก ์„ค์ • (๊ฐ์ฒด ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ String ํ˜•ํƒœ๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌ)
const jsonParser = express.json({ strict: false });
const port = 4999;

app.use(cors());

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

app.post("/upper", jsonParser, (req, res) => {
  res.json(req.body.toUpperCase());
});

app.post("/lower", jsonParser, (req, res) => {
  res.json(req.body.toLowerCase());
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});


์˜ค๋Š˜ ํŽ˜์–ด๋ถ„๊ณผ ํ•จ๊ป˜ StatesAirline Server๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ํ•ญ๊ณตํŽธ ์กฐํšŒ ๋ฐ ์˜ˆ์•ฝ ์„œ๋ฒ„ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.
์•„์ง ๋๊นŒ์ง€ ๊ตฌํ˜„ํ•˜์ง€๋Š” ๋ชป ํ•ด์„œ ์›”์šœ์— ๋งˆ์ € ๋‹ค ๊ตฌํ˜„ํ•˜๊ณ  ์ข€ ๋” ์ดํ•ดํ•œ ๋‹ค์Œ์— ๋ธ”๋กœ๊น…ํ•˜์ขŒ
์ฒ˜์Œ์— ์ง„์งœ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ์ง€ ๋„˜ ๋ง‰๋ง‰ํ–ˆ๋Š”๋ฐ ๊ฐ™์ด ์•„๋ฌด๊ฑฐ๋‚˜ ๋•Œ๋ ค๋„ฃ์–ด๋ณด์ž ๋ผ๋Š” ๋งˆ์ธ๋“œ๋กœ ์ƒ๊ฐํ•œ๋Œ€๋กœ ์ฝ”๋“œ ๋•Œ๋ ค๋„ฃ์–ด์„œ ์–ด์ฐŒ์–ด์ฐŒ 10/13 testcase pass๐Ÿซ 
์›”์šœ์— ๋งˆ์ € ๋ชจ๋“  testcase ํ†ต๊ณผํ•˜๊ธฐ!

๋‚ด์ผ๋ถ€ํ„ฐ ์ฃผ๋ง๋™์•ˆ ์•Œ๋ฐ” ์‹œ๊ฐ„ ์ œ์™ธํ•˜๊ณ  ์งฌ์งฌ์ด React๋กœ CRUD ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ! ์•„์ง ์ข€ ๋ง‰๋ง‰ํ•˜์ง€๋งŒ ์ผ๋‹จ ๋ญ๋“  ์ ์–ด๋ณด๋ฉด ์–ด๋–ป๊ฒŒ๋“  ๊ตฌํ˜„๋˜์ง€ ์•Š์„๊ฝˆ,,

์š”์ฆ˜ ๋‚ด์šฉ์ด ์–ด๋ ค์›Œ์ ธ์„œ ๋”ฐ๋ผ๊ฐ€๊ธฐ ๊ธ‰๊ธ‰ํ•˜๋‹ฌ๊นŒ.. ๊ทธ๋ž˜์„œ ๊ทธ๋Ÿฐ์ง€ ์•”๊ฒƒ๋„ ํ•˜๊ธฐ์‹ซ๊ณ  ์ถ•์ถ• ์ณ์ง€๋Š” ๋Š๋‚Œ ๐Ÿฅฒ

profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€