9/2 TIL : ์ ๊ฐ ๊ตฌ๋ฌธ, yarn, postman, express router
๐ ์ ๊ฐ๊ตฌ๋ฌธ
๐ฉ ๊ฐ์ฒด ํฉ์น๊ธฐ
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}`);
});