๐ JSDoc /** */
- ํน์ ํํ๋ก ์ฃผ์์ ๋ฌ๋ฉด ์ฃผ์ ๋ด์ฉ์ ํตํด ํจ์์ ๊ธฐ๋ฅ, ๋งค๊ฐ ๋ณ์ ํ์ ๋ฑ์ ๋ฏธ๋ฆฌ ์๋ ค์ค ์ ์์
- ๊ฑฐ์ ํ์คํ๊ฐ ๋์ด์ vsCode, webstorm ๋ฑ์์ ๊ธฐ๋ณธ์ผ๋ก ๊ธฐ๋ฅ ์ ๊ณต
/**
* @param {string} name ์ด๋ฆ
* @param {number} age ๋์ด
* @returns ์ด๋ฆ๊ณผ ๋์ด๋ฅผ ๋ฐ์์ ๋ฌธ์์ด๋ก ์ถ๋ ฅํฉ๋๋ค.
* @todo ์๋ก์ด ๋งค๊ฐ๋ณ์ ์ถ๊ฐ
* @deprecated
*/
function hello(name, age) {
return `๋ด ์ด๋ฆ์ ${name}์ด๊ณ ๋์ด๋ ${age}์ธ ์
๋๋ค.`;
}
๐ฉ JSDoc ๋ถ๊ฐ ๊ธฐ๋ฅ
- @todo : ๋ฉ๋ชจ์ฒ๋ผ ์ฌ์ฉ
- @deprecated : ๋ ์ด์ ํด๋น ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ง ์์ ๋
- @type { ํ์ ํํ } : ๋ณ์์ ํน์
- @typedef : JSDoc์ ๋ด์ฉ์ ๋ณ์์ฒ๋ผ ์ ์ธํด ๋๊ณ @type { }๋ก ๋ค๋ฅธ ๊ณณ์์ ์ฌ์ฉ ๊ฐ๋ฅ
/** @type {string} */
const test = 'hy';
/**
* @typedef Post
* @property {number} id
* @property {string} title
* @property {string} content
*/
/** @type {Post} */
const post = {
id: 1,
title: '์ ๋ชฉ',
content: '๋ด์ฉ',
};
๐ ํ๋ ์์ํฌ ์์ด ๋ธ๋ก๊ทธ CRUD ๊ตฌํํ๊ธฐ
// @ts-check
const http = require('http');
// DB ์์ผ๋ฏ๋ก ๋ฐ์ดํฐ ๋ณ์ ์ ์ธ
const posts = [
{
id: 1,
title: 'first',
content: 'hello, back-end',
},
{
id: 2,
title: 'second',
content: 'hello, back-end',
},
{
id: 3,
title: 'third',
content: 'hello, back-end',
},
];
// ์๋ฒ ์ ์
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json; charset=utf-8');
const urlArr = req.url ? req.url.split('/') : [];
let id = -1;
if (urlArr.length > 2) id = parseInt(urlArr[2], 10);
/**
* ๋ธ๋ก๊ทธ์ฉ ์๋ฒ API ๊ตฌ์ฑ
*
* GET /posts ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
* GET /posts/:id ๊ธ ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ
* POST /posts ์๋ก์ด ๊ธ ์ฌ๋ฆฌ๊ธฐ
* PUT /posts/:id ๊ธฐ์กด ๊ธ ์์ ํ๊ธฐ
* DELETE /posts/:id ๊ธฐ์กด ๊ธ ์ญ์ ํ๊ธฐ
*/
if (req.url === '/posts' && req.method === 'GET') {
const result = {
posts: posts.map((post) => ({
id: post.id,
title: post.title,
content: post.content,
})),
totalCount: posts.length,
};
res.statusCode = 200;
res.end(JSON.stringify(result));
console.log('๋ธ๋ก๊ทธ์ ๊ธ ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๋ API ์
๋๋ค.');
} else if (id !== -1 && req.method === 'GET') {
const result = posts.find((post) => post.id === id);
if (result) {
res.statusCode = 200;
res.end(JSON.stringify(result));
console.log('๋ธ๋ก๊ทธ์ ํน์ ๊ธ ๋ด์ฉ์ ๊ฐ์ ธ์ค๋ API ์
๋๋ค.');
} else {
res.statusCode = 404;
res.end('ํฌ์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.');
console.log('ํด๋น id๋ฅผ ๊ฐ์ง๋ ํฌ์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.');
}
} else if (req.url === '/posts' && req.method === 'POST') {
req.setEncoding('utf-8');
req.on('data', (data) => {
const newPost = JSON.parse(data);
posts.push({
id: posts[posts.length - 1].id + 1,
title: newPost.title,
content: newPost.content,
});
});
res.statusCode = 200;
res.end('์๋ก์ด ๊ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.');
console.log('๋ธ๋ก๊ทธ์ ๊ธ์ ์ฌ๋ฆด ๋ ํธ์ถํ API ์
๋๋ค.');
} else if (id !== -1 && req.method === 'PUT') {
const result = posts.find((post) => post.id === id);
if (result) {
req.setEncoding('utf-8');
req.on('data', (data) => {
const modiPost = JSON.parse(data);
if (modiPost.title) posts[id - 1].title = modiPost.title;
if (modiPost.content) posts[id - 1].content = modiPost.content;
});
res.end('๊ธ์ด ์์ ๋์์ต๋๋ค.');
res.statusCode = 200;
console.log('๋ธ๋ก๊ทธ์ ๊ธ์ ์์ ํ ๋ ํธ์ถํ API ์
๋๋ค.');
} else {
res.statusCode = 404;
res.end('ํฌ์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.');
console.log('ํด๋น id๋ฅผ ๊ฐ์ง๋ ํฌ์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.');
}
} else if (id !== -1 && req.method === 'DELETE') {
const result = posts.find((post) => post.id === id);
if (result) {
posts.splice(id - 1, 1);
res.statusCode = 200;
res.end('๊ธ์ด ์ญ์ ๋์์ต๋๋ค.');
console.log('๋ธ๋ก๊ทธ์ ๊ธ์ ์ญ์ ํ ๋ ํธ์ถํ API ์
๋๋ค.');
} else {
res.statusCode = 404;
res.end('ํฌ์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.');
console.log('ํด๋น id๋ฅผ ๊ฐ์ง๋ ํฌ์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.');
}
} else {
res.statusCode = 400;
res.end('NOT FOUND');
console.log('ํด๋น API๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.');
}
});
// ์๋ฒ ์คํ
const PORT = 4000;
server.listen(PORT, () => {
console.log(`ํด๋น ์๋ฒ๋ ${PORT}๋ฒ ํฌํธ์์ ์๋ ์ค์
๋๋ค.`); // ์๋ฒ ์ฝ์
});
๐ Code Refactoring: ํ๋ก๊ทธ๋จ์ ๋์์ ๋ณํ์ํค์ง ์๊ณ ๋ด๋ถ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ ํ๋๊ฒ
- ์์๊ธฐ์ -> ์๋ก์ด ๊ธฐ์
- ์ฝ๋ ๋นํจ์จ -> ํจ์จ(๋ฐ๋ณต๋ฌธ ์ค์ด๊ธฐ, ์ค๋ณต ๊ธฐ๋ฅ ์ ๊ฑฐ, ๋ฉ๋ชจ๋ฆฌ ํจ์จ ์ฆ๋)
- ์ฝ๋ ๊ฐ๋ ์ฑ, ํ์ฅ์ฑ ๋์ด๊ธฐ
๐ JS Module
๐ฉ CommonJS ๋ฐฉ์
- node.js์์ ์ฌ์ฉ๋๋ ๋ชจ๋ ๋ฐฉ์
- require / exports
1. ์ ์ฒด ๋ชจ๋๋ก์จ ๋ด๋ณด๋ด๊ณ , ์ ์ฒด๋ฅผ ํ๋์ obj๋ก ๋ฐ์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
// animal.js
const animals = ['dog', 'cat'];
function showAnimals() {
animals.map((el) => console.log(el));
}
module.exports = {
animals,
showAnimals,
};
// module.js
const animals = require('./animals');
console.log(animals.animals);
animals.showAnimals();
2. ๋ด๋ณด๋ด๊ณ ์ถ์ ๊ฒ(๋ณ์, ํจ์, ํด๋์ค ๋ฑ)์ exports๋ฅผ ๋ถ์ฌ์ ๋ด๋ณด๋ด๊ณ , ๊ฐ๊ฐ์ ๋ฐ๋ก ์ ์ธํด์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ
// animal.js
const animals = ['dog', 'cat'];
exports.animals = animals;
exports.showAnimals = function showAnimals() {
animals.map((el) => console.log(el));
};
// module.js
const { animals, showAnimals } = require('./animals');
console.log(animals);
showAnimals();
๐ฉ ES6๋ฐฉ์
- 2015๋ ์ ES6๊ฐ ์ ๋ฐ์ดํธ๋๋ฉด์ ์ถ๊ฐ๋ ๋ฐฉ์
- ๋ธ๋ผ์ฐ์ ์์ ๊ตฌ๋๋๋ js์ ๋ํ ๋ฌธ๋ฒ์ด๊ธฐ ๋๋ฌธ์ node.js์์ ์ฌ์ฉํ๋ ค๋ฉด ๋ณ๋ ์ฒ๋ฆฌ ํ์
- package.json์ "type": "module" ์ถ๊ฐํ๋ฉด ์ฌ์ฉ ๊ฐ๋ฅ
- import / export
1. ์ ์ธ๋ถ์ export ์ฌ์ฉ
// animal.js
export const animals = ['dog', 'cat'];
export function showAnimals() {
animals.map((el) => console.log(el));
}
// module.js
import { animals, showAnimals } from './animals.js';
console.log(animals);
showAnimals();
2. ๋ง์ง๋ง์ export ์ฌ์ฉ
// animal.js
const animals = ['dog', 'cat'];
function showAnimals() {
animals.map((el) => console.log(el));
}
export { animals, showAnimals };
// module.js
import { animals, showAnimals } from './animals.js';
console.log(animals);
showAnimals();
- ๊ฐ์ ธ์ฌ ๊ฒ๋ค์ด ๋ง์ผ๋ฉด * as๋ฅผ ์ฌ์ฉ
import * as animals from './animals.js';
console.log(animals.animals);
animals.showAnimals();
- ๋ชจ๋์ด๋ฆ์ ๋ฐ๊พธ๊ณ ์ถ์ผ๋ฉด ๋ชจ๋์ด๋ฆ as ์๋ก์ด๋ชจ๋์ด๋ฆ ์ผ๋ก ๋ณ๊ฒฝ
// import
import { animals as ani, showAnimals as show } from './animals.js';
console.log(ani);
show();
// export
const animals = ['dog', 'cat'];
function showAnimals() {
animals.map((el) => console.log(el));
}
export { animals as ani, showAnimals as show };
1. ๋ณต์์ export๊ฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํํ์ ๋ชจ๋
2. ํ์ผ ํ๋์ ํ๋์ ๊ฐ์ฒด(obj, class)๋ง ์๋ ๋ชจ๋ - export default
// animal.js
export default class Animal {
constructor() {
this.animals = ['dog', 'cat'];
}
showAnimals() {
this.animals.map((el) => console.log(el));
}
}
// module.js
import Animal from './animals.js';
const ani = new Animal();
console.log(ani.animals);
ani.showAnimals();
'KDT > TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
9/2 TIL : ์ ๊ฐ ๊ตฌ๋ฌธ, yarn, postman, express router (0) | 2022.09.18 |
---|---|
8/31 TIL : File-system(JS, Promise, async/await), Routing w/o framework (0) | 2022.09.18 |
8/26 TIL : class, CRUD (0) | 2022.09.14 |
8/24 TIL : node.js, promise, async/await (0) | 2022.09.07 |
8/5 TIL : Git branch/merge/ํ์ (0) | 2022.09.05 |