[THU] 2021-12-23 TIL

MINBOKยท2021๋…„ 12์›” 23์ผ
0

๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ

๋ชฉ๋ก ๋ณด๊ธฐ
16/19
post-thumbnail
post-custom-banner

Git ๋ช…๋ น์–ด

  • git branch one : ๋ธŒ๋žœ์น˜ ์ƒ์„ฑ
  • git checkout one : one ์ด๋ผ๋Š” ๋ธŒ๋žœ์น˜๋กœ ๋“ค์–ด๊ฐ
  • git checkout one : ๋ฉ”์ธ์œผ๋กœ ๋Œ์•„๊ฐ
  • git push origin one : one ์ด๋ผ๋Š” ๋ธŒ๋žœ์น˜์— push
  • git merge one : one์ด๋ผ๋Š” ๋ธŒ๋žœ์น˜์™€ merge

๐Ÿ˜ข fork/pull request

Node.js

โœจ Node.js ๊ณต์‹ ๋ฌธ์„œ
ย ย https://nodejs.org/dist/latest-v16.x/docs/api/index.html

โœจNode.js ํ•™์Šต
ย ย https://nodejs.dev/learn

01. Node.js๋ž€?

: Javascript ๋Ÿฐํƒ€์ž„ (Javascript ์‹คํ–‰๊ธฐ/์‹คํ–‰ ์†Œํ”„ํŠธ์›จ์–ด)

๐Ÿ˜ข REPL(Read, Eval, Print, Loop)

02. ๋ชจ๋“ˆ

ย ย ย ย 2.1 ๋ชจ๋“ˆ์ด๋ž€?
ย ย ย ย ย ย ย ย ย : ํŠน์ •ํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ์ฝ”๋“œ ๋ญ‰ํ……์ด(๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ)

ย ย ย ย 2.2 ๋ชจ๋“ˆ์˜ ์ข…๋ฅ˜ (3๊ฐ€์ง€)
ย ย ย ย ย ย ย ย ย 1. ์‚ฌ์šฉ์ž ์ž‘์„ฑ ๋ชจ๋“ˆ
ย ย ย ย ย ย ย ย ย 2. ๋…ธ๋“œ ๋‚ด๋ถ€ ๋ชจ๋“ˆ (์„ค์น˜ ํ•„์š”์—†์Œ)
ย ย ย ย ย ย ย ย ย 3. ๋…ธ๋“œ ์™ธ๋ถ€ ๋ชจ๋“ˆ (์„ค์น˜ ํ•„์š”)

03. simple server

  • ctrl + shift + ` : VSC์—์„œ ํ„ฐ๋ฏธ๋„ ์—ด๊ธฐ
  • ctrl + c : ์—ด๋ฆฐ ์„œ๋ฒ„ ๋‹ซ๊ธฐ
  • node ์ด๋ฆ„ : ์„œ๋ฒ„ ์ผœ๊ธฐ
  • http://127.0.0.1:ํฌํŠธ๋ฒˆํ˜ธ/ : url ์ ‘์†ํ•ด์„œ ํ™•์ธํ•˜๊ธฐ
    (app.listen(ํฌํŠธ๋ฒˆํ˜ธ);) : ํฌํŠธ๋ฒˆํ˜ธ ๊ฐ™์•„์•ผํ•จ
  • ์ƒํƒœ์ฝ”๋“œ (ex.404)๋Š” ์ ˆ๋Œ€์ ์ธ๊ฒƒ์€ ์•„๋‹˜
    ย -> ๋ณด์•ˆ์„ ์œ„ํ•ด ๊ผฌ์•„๋‘๊ธฐ๋„ ํ•จ

04. npm

  • ๋‹ค์–‘ํ•œ ๋ชจ๋“ˆ๋“ค์„ ๋ชจ์•„๋†“์€ ๊ณณ
  • npm init --yes : package.jsonํŒŒ์ผ์ด ์ƒ์„ฑ๋จ

05. import from

  • export : ๋ฐ–์—์„œ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ์Œ
  • importย { }ย fromย './ํŒŒ์ผ์ด๋ฆ„.js'; : ํŒŒ์ผ์ด๋ฆ„.js์—์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ์Œ
    (= constย ๊ฐœ์ธํ”„๋กœํ•„ย =ย require('./two');)
    ย ย ย => ์‹ค๋ฌด์—์„œ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค (bc. ์˜›๋‚  ๋ฐฉ์‹)
-- one

import {์ด๋ฆ„, ๋‚˜์ด, ํ•œ์‚ด๋”๋จน์Œ, ๊ฐ’๊ฐ€์ ธ์™€} from './two.js';

console.log(์ด๋ฆ„);
console.log(๋‚˜์ด);
ํ•œ์‚ด๋”๋จน์Œ();
ํ•œ์‚ด๋”๋จน์Œ();
console.log(๋‚˜์ด);
console.log(๊ฐ’๊ฐ€์ ธ์™€());
  • importย * as ๊ฐœ์ธํ”„๋กœํ•„ย fromย './ํŒŒ์ผ์ด๋ฆ„.js'; : ์ „์ฒด๋ชจ๋“ˆ(*)์„ ๊ฐœ์ธํ”„๋กœํ•„์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ง€๊ณ  ์˜ค๊ฒ ๋‹ค
-- two

import * as ๊ฐœ์ธํ”„๋กœํ•„ from './two.js';
// const ๊ฐœ์ธํ”„๋กœํ•„ = require('./two');

console.log(๊ฐœ์ธํ”„๋กœํ•„.์ด๋ฆ„);
console.log(๊ฐœ์ธํ”„๋กœํ•„.๋‚˜์ด);
๊ฐœ์ธํ”„๋กœํ•„.ํ•œ์‚ด๋”๋จน์Œ();
๊ฐœ์ธํ”„๋กœํ•„.ํ•œ์‚ด๋”๋จน์Œ();
console.log(๊ฐœ์ธํ”„๋กœํ•„.๋‚˜์ด); //์ด๋ฒˆ์—๋Š” ์ถœ๋ ฅ์ด ๋˜์—ˆ์ฃ ?
console.log(๊ฐœ์ธํ”„๋กœํ•„.๊ฐ’๊ฐ€์ ธ์™€());

* one๊ณผ two์˜ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Œ

05. Path(๊ฒฝ๋กœ)

โœจ๊ณต์‹๋ฌธ์„œ ์ฐธ๊ณ 
https://nodejs.org/dist/latest-v16.x/docs/api/path.html

console.log(`๊ตฌ๋ถ„์ž : ${path.sep}`); // ๊ตฌ๋ถ„์ž : \
console.log(`๋””๋ ‰ํ† ๋ฆฌ : ${path.dirname(__filename)}`); // ๋””๋ ‰ํ† ๋ฆฌ: ํŒŒ์ผ์ฃผ์†Œ
console.log(`ํŒŒ์ผ์ด๋ฆ„ : ${path.basename(__filename)}`); // ํŒŒ์ผ์ด๋ฆ„ : app.js
console.log(`ํ™•์žฅ์ž : ${path.extname(__filename)}`); // ํ™•์žฅ์ž : .js
console.log(__filename);
console.log(__dirname);
console.table(path.parse(__filename));
console.log(path.join(__dirname, 'source'))
  • join : ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ ํ•ฉ์น˜๊ธฐ(์ž์ฃผ ์‚ฌ์šฉํ•จ)
console.log(path.join(__dirname, 'source')); 
console.log(path.join(__dirname, 'app.js')); // ์‚ฌ์šฉํ•˜์„ธ์š”

console.log(__dirname+'/node'); // ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. => ์˜ค๋ฅ˜๋‚จ

06. fs(file_system) ๋ชจ๋“ˆ

6.1 rename, ํŒŒ์ผ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ

const fs = require('fs');

let ๋ณ€์ˆ˜ = 'hodu' // ์‚ฌ์šฉ์ž ์ด๋ฆ„
let ๋‚ ์งœ = new Date()
fs.rename('./test.txt', `./${๋ณ€์ˆ˜}${๋‚ ์งœ.getMilliseconds()}.txt`, (err) => {
  console.log(err);
})

=> test.txt ํŒŒ์ผ์˜ ์ด๋ฆ„์ด ํ˜ธ๋‘36.txt๋กœ ๋ณ€๊ฒฝ๋จ


6.2 readFile, ํŒŒ์ผ ๋‚ด์šฉ ์ฝ์–ด์˜ค๊ธฐ

fs.readFile('./test.txt', 'utf8', (err, data) => {
     console.log(err);
     console.log(data);
 })

=> test.txt ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์˜ด


6.3 writeFile, ์ƒˆ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

 fs.writeFile('./test2.txt', 'hello world 2', (err)=>{
     console.log(err)
 });

=> hello world 2๋ผ๋Š” ๋‚ด์šฉ์„ ๊ฐ€์ง„ test2.txt ํŒŒ์ผ์„ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•จ


6.4 appendFile, ํŒŒ์ผ์•ˆ์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ

 fs.appendFile('./test2.txt', 'hello world 3', (err)=>{
     console.log(err)
 });

=> test2.txtํŒŒ์ผ์•ˆ์— hello world 3์ด๋ผ๋Š” ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
ย ย ย ย ย (์ค„ ๋ฐ”๊ฟˆX)

โ“ ๋งŒ์•ฝ ์ค„ ๋ฐ”๊ฟˆ์„ ์›ํ•œ๋‹ค๋ฉด

fs.appendFile('./test2.txt', '\nhello world 3', (err)=>{
     console.log(err)
 });

=> \n๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋‚ด์šฉ์ด ์ค„ ๋ฐ”๊ฟˆ๋˜์–ด ์ถ”๊ฐ€๋œ๋‹ค.

ย ย ย โœ” CRLF

ย ย ย ย  \n (๊ฐœํ–‰๋ฌธ์ž) : ์ค„ ๋ฐ”๊ฟˆ ์ฒ˜๋ฆฌ
ย ย ย ย  \w (ใ…‘ใ…‘๋ฌธ์ž) : ์ค„ ๋ฐ”๊ฟˆ ์ฒ˜๋ฆฌ

6.5 copyFile, ํŒŒ์ผ์„ copyํ•ด์„œ ๋‹ค๋ฅธ ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ

 fs.copyFile('./test2.txt', './test3.txt', (err)=>{
     console.log(err)
 });

=> test2์™€ ๋‚ด์šฉ์ด ๋™์ผํ•œ test3๋ฅผ ์ƒ์„ฑํ•จ


6.6 mkdir, ์ƒˆ๋กœ์šด ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑํ•˜๊ธฐ

fs.mkdir('./yoyo', (err)=>{
    console.log(err)
});

=> yoyo๋ผ๋Š” ์ด๋ฆ„์˜ ์ƒˆ๋กœ์šด ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ

โ— ์—๋Ÿฌ ๋ฐœ์ƒ (-4058)
ย  &nbserrno: -4058,
ย ย code: 'ENOENT',
ย  syscall: 'rename',
ย  path:
ย  dest:

=> './test.txt'๋ฅผ 'test.txt'๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ์ •์ƒ์ž‘๋™ํ•จ

07. http ๋ชจ๋“ˆ

const http = require('http'); // ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ด
const server = http.createServer(function(req, res){
    res.write(`
    <!DOCTYPE html>
    <html>
    <head>
        <title>test</title>
    </head>
    <body>
        <form action="/" method="get">
            id : <input type="text" name='id_value'><br>
            pw : <input type="password" name='pw_value'><br>
            <input type="submit" value="login">
        </form>
    </body>
    </html>
    `);
    res.end();
});

server.listen(8080);
  • const http = require('http');
    => ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ด, http๋ผ๋Š” ๋ณ€์ˆ˜๋กœ http ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.

๐Ÿ˜ข const server = http.createServer(function(req, res)
๐Ÿ˜ข writeHead์™€ end๋Š” ์„ธํŠธ

08. get/post

๐Ÿ˜ข

  • get ๋ฐฉ์‹: url์— ์ฐํž˜, 127~url ์ž…๋ ฅ์€ get๋ฐฉ์‹,
    ๊ฐœ์ธ์ •๋ณด get๋ฐฉ์‹์œผ๋กœ ๋ฐ›์œผ๋ฉด ์•ˆ๋จ
    ย bc. ํ„ฐ๋ฏธ๋„์— ๋“ค์–ด์˜จ ์ž…๋ ฅ๋œ ๊ฐ’์ด ์ฐํž˜,
  • post ๋ฐฉ์‹: url์— ์•ˆ์ฐํž˜ => ์ž…๋ ฅ๋œ ๊ฐ’์€ ์–ด๋””์—...?

โœจpost๊ด€๋ จ ๊ณต์‹๋ฌธ
https://nodejs.org/ko/docs/guides/anatomy-of-an-http-transaction/

๐Ÿ˜ขconst querystring = require('querystring');
โœจ ๊ด€๋ จ ๋ธ”๋กœ๊ทธ๊ธ€
https://codedragon.tistory.com/2704
โœจ ๊ณต์‹๋ฌธ์„œ
https://nodejs.org/dist/latest-v16.x/docs/api/querystring.html

Q. ๊ถ๊ธˆ

๐Ÿ˜ข fork/pull request
๐Ÿ˜ข REPL(Read, Eval, Print, Loop)
๐Ÿ˜ขconst server = http.createServer(function(req, res){ });
ย ย ย ย https://velog.io/@minbok/Node.js-http-%EB%AA%A8%EB%93%88
๐Ÿ˜ข get/post
๐Ÿ˜ข querystring
๐Ÿ˜ข ๋ฉ”์„œ๋“œ์™€ ์ด๋ฒคํŠธ์˜ ์ฐจ์ด
๐Ÿ˜ข ์ฝœ๋ฐฑํ•จ์ˆ˜

post-custom-banner

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