KDT/TIL

9/2 TIL : ์ „๊ฐœ ๊ตฌ๋ฌธ, yarn, postman, express router

ebulsok 2022. 9. 18. 19:26

๐Ÿ”Ž ์ „๊ฐœ๊ตฌ๋ฌธ

๐Ÿšฉ ๊ฐ์ฒด ํ•ฉ์น˜๊ธฐ

const bulsokData = {
  name: 'ebulsok',
  gender: 'F',
};

const bulsokInfo = {
  nickName: 'bulsok',
  email: 'leebulsok@gmail.com',
};

const bulsok = {
  ...bulsokData,
  ...bulsokInfo,
};

console.log(bulsok);

 

๐Ÿšฉ ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ

const arr1 = [1, 2, 3, 4, 5];
const arr2 = ['6', '7', '8'];
const merge = [...arr1, ...arr2];
console.log(merge); // [1, 2, 3, 4, 5, '6', '7', '8']

 

๐Ÿšฉ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž, ๊ฐ์ฒด

const bulsokData = {
  name: 'ebulsok',
  gender: 'F',
  nickName: 'bulsok',
  email: 'leebulsok@gmail.com',
};

const { name, ...bulsokInfo } = bulsokData;
console.log(name, bulsokInfo); // ebulsok { gentder: 'F', nickname: 'bulsok', email: 'leebulsok@gmail.com' }

๐Ÿ”Ž Yarn: facebook์—์„œ ๋งŒ๋“  ๋…ธ๋“œ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž ๋„๊ตฌ

  • ์†๋„: ํŒจํ‚ค์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์บ์‹œ์— ์ €์žฅํ•˜์—ฌ ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์šด๋ฐ›์ง€ ์•Š๊ณ  ์„ค์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋” ๋น ๋ฆ„
  • ์•ˆ์ •์„ฑ/๋ณด์•ˆ์„ฑ: ์ด์ „์— npm์ด ์ œ๊ณตํ•˜์ง€ ์•Š๋˜ yarn.lock ํŒŒ์ผ์„ ์ œ๊ณต. ๊ฐ„๋‹จํ•œ ์ •๋ณด๋งŒ ํฌํ•จํ•˜๋˜ package.json์ด ์•„๋‹ˆ๋ผ ๊ฐ ํŒจํ‚ค์ง€์˜ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ์ „๋ถ€ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋А ํ™˜๊ฒฝ์—์„œ๋‚˜ ๊ฐ™์€ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณด์žฅํ•จ => npm๋„ package-lock.json์„ ๋„์ž…ํ•˜์—ฌ ๋ฌธ์ œ ํ•ด๊ฒฐ
  • [ํ„ฐ๋ฏธ๋„] npm install -g yarn
  • [ํ„ฐ๋ฏธ๋„] yarn init -y / npm init -y

 

  • [ํ„ฐ๋ฏธ๋„] yarn add -D prettier / npm install --save-dev prettier
  • .prettierrc ํŒŒ์ผ ์„ค์ •
{
    "semi": true,
    "singleQuote": true
}
  • .vscode/settings.json ์„ค์ •
{
    "[javascript]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    }
}

 

  • [ํ„ฐ๋ฏธ๋„] yarn add -D eslint / npm install --save-dev eslint
  • [ํ„ฐ๋ฏธ๋„] yarn add -D eslint-config-airbnb-base eslint-plugin-import / npm install --save-dev eslint-config-airbnb-base eslint-plugin-import
  • .eslintrc.js ํŒŒ์ผ ์„ค์ •
module.exports = {
  extends: ['airbnb-base'],
  rules: {
    'linebreak-style': 0, // ์œˆ๋„์šฐ ํ•„์ˆ˜
    'no-console': 'off', // ์ฝ˜์†”๋กœ๊ทธ ์‚ฌ์šฉ
    'prefer-destructuring': 'off', // ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น๋ฌธ๋ฒ•
  },
};

 

  • [ํ„ฐ๋ฏธ๋„] yarn add -D typescript / npm install --save-dev typescript
  • [ํ„ฐ๋ฏธ๋„] yarn add -D @types/node / npm install --save-dev @types/node
  • ์‚ฌ์šฉ ์‹œ // @ts-check ์ถ”๊ฐ€

 

  • [ํ„ฐ๋ฏธ๋„] yarn add express / npm install express
  • [ํ„ฐ๋ฏธ๋„] yard add -D @types/express / npm install --save-dev @types/express

 

  • [ํ„ฐ๋ฏธ๋„] yarn add -D nodemon / npm install --save-dev nodemon
  • package.json์— ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€
"scripts": {
    "server": "nodemon src/main.js",
    "dev": "nodemon --watch \"./src/*.js\" --exec \"npm run server\""
  },

 

๐Ÿ”Ž Node.js ๋‚ด์žฅ ๊ฐ์ฒด ์‚ฌ์šฉํ•˜๊ธฐ

  • ํ•ด๋‹น ํŒŒ์ผ์˜ ์œ„์น˜ ์ •๋ณด: __dirname
  • ํ•ด๋‹น ํŒŒ์ผ์˜ ์œ„์น˜+ํŒŒ์ผ๋ช… ์ •๋ณด: __filename

 

๐Ÿ”Ž Postman: ์„œ๋ฒ„ ์š”์ฒญ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

https://www.postman.com/downloads/

 

๐Ÿ”Ž Middleware: ์„œ๋กœ ๋‹ค๋ฅธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜(ํ”„๋กœ๊ทธ๋žจ)์ด ์„œ๋กœ ํ†ต์‹ ์„ ํ•˜๋Š”๋ฐ์— ์‚ฌ์šฉ๋˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด

  • express์—์„œ์˜ ๋ฏธ๋“ค์›จ์–ด๋Š” ์„œ๋ฒ„์— ๋“ค์–ด์˜จ ์š”์ฒญ์ด ๋“ค์–ด์™€์„œ ์‘๋‹ต์œผ๋กœ ๋‚˜๊ฐˆ ๋•Œ๊นŒ์ง€ ๊ฑฐ์น˜๋Š” ๋ชจ๋“  ํ•จ์ˆ˜ ๋˜๋Š” ๊ธฐ๋Šฅ์„ ์˜๋ฏธ
  • app.use ๋˜๋Š” app.method ํ•จ์ˆ˜๋ฅผ ์ด์šฉ app.use('์š”์ฒญ์ฃผ์†Œ', (req, res, next) => { });
// @ts-check
const express = require('express');

const app = express();
const PORT = 4000;

app.use('/', async (req, res, next) => {
  res.send('Hello, Express world!');
});

app.listen(PORT, () => {
  console.log(`The express server is running at port: ${PORT}`);
});

 

๐Ÿšฉ next: callback ํ•จ์ˆ˜๋กœ์„œ ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ํ˜„์žฌ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ๋‹ค์Œ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์‹คํ–‰์‹œํ‚ด

  • res.send๋กœ ํ†ต์‹ ์ด ์ข…๋ฃŒ๋˜๋„ next๋Š” ์‹คํ–‰๋จ
  • ์ด์ „ ๋ฏธ๋“ค์›จ์–ด์—์„œ ์ฒ˜๋ฆฌํ•œ ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ req ๊ฐ์ฒด์— ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋งŽ์ด ์‚ฌ์šฉ๋จ
// @ts-check
const express = require('express');

const app = express();
const PORT = 4000;

app.use('/', async (req, res, next) => {
  console.log('๋ฏธ๋“ค์›จ์–ด 1๋ฒˆ');
  next();
});

app.use((req, res, next) => {
  console.log('๋ฏธ๋“ค์›จ์–ด 2๋ฒˆ');
  res.send('res.send!');
  next();
});

app.use((req, res, next) => {
  console.log('๋ฏธ๋“ค์›จ์–ด 3๋ฒˆ');
  res.send('ํ†ต์‹  ์ข…๋ฃŒ');
});

app.listen(PORT, () => {
  console.log(`The express server is running at port: ${PORT}`);
});

 

๐Ÿšฉ req.params: ๋ฐ›์„ url์—์„œ :ํŒŒ๋ผ๋ฏธํ„ฐ๋ช… ์„ ๋ฏธ๋ฆฌ ์ •์˜ ํ•˜๋ฉด data๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

app.get('/id/:id/name/:name/gender/:gender', (req, res) => {
  console.log(req.params);
  res.end(req.params);
});

 

๐Ÿšฉ req.query: url์— ? ๋ฅผ ๋ถ™์ธ ๋’ค ํ•„๋“œ๋ช…=๊ฐ’ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ •์˜๋˜์ง€ ์•Š์€ ํ˜•ํƒœ๋กœ๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

  • ex. localhost:4000?test=test&id=1

 

๐Ÿ”Ž express.router

express์˜ routing์„ ์œ„ํ•œ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ด์šฉ

// @ts-check
const express = require("express");

const app = express();
const PORT = 4000;
const userRouter = express.Router();

app.use("/users", userRouter);

const USER = [
  {
    id: "bulsok",
    name: "ebulsok",
  },
  {
    id: "test",
    name: "ํ…Œ์ŠคํŠธ",
  },
];

userRouter.get("/", (req, res) => {
  res.end("ํšŒ์› ๋ชฉ๋ก");
});

userRouter.get("/:id", (req, res) => {
  res.end("ํŠน์ • ํšŒ์› ์ •๋ณด");
});

userRouter.post("/", (req, res) => {
  res.end("ํšŒ์› ๋“ฑ๋ก");
});

app.use("/", (req, res) => {
  res.end("Hello, express world!");
});

app.listen(PORT, () => {
  console.log(`The express server is running at port: ${PORT}`);
});