#39 ์ amend, stash, cherry-pick, pull request ๋ฅผ ์ด์ด์ ์ ๋ฆฌํ๋ค.
-> ๊ฐ๋จํ ๋ก์ง, ๋น ๋ฅธ ์๋ต์๊ฐ, ๋น ๋ฅธ ๊ฐ๋ฐ, ๋น๋๊ธฐ ๋ฐฉ์ ์ผ๋ก ์ด์๋์ด์ผํ๋ ์๋น์ค์ ์ฌ์ฉํ๊ธฐ ์ข๋ค.
-> ์ค์นํด์ ์ฌ์ฉํ๋ฉด ๋๊ณ , ํน์ ์ค์น๊ฐ ๋ถ๊ฐ๋ฅํ ํ๊ฒฝ์ด๋ฉด ๊ตฌ๋ฆIDE, AWS ๋ฑ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
node
# node ๋ฐํ์์์ ์ง์ ์ฝ๋ ์
๋ ฅํด์ ์คํ ๊ฐ๋ฅ
node filename
# jsํ์ผ์ ์คํ์์ผ์ค. - ์ด๋ค ํ์ด์ง์ ๊ฐ์ ๋ ์ด๋ค jsํ์ผ์ ์คํํ ์ง ๊ฐ์ ๊ฒ์ node๋ก ์ ์ด
> console.log('hello world'); # node ์ console ์ global.console.
๋ชจ๋์ ๋ง๋ค๊ณ ๊ทธ ๋ชจ๋์ ์ ์ดํ๋ jsํ์ผ์ ํ๋๋ฌ์ ๋ชจ๋์ ์คํํ๋ ๊ตฌ์กฐ๋ก ๋ง๋ ๋ค.
// ์ ์ดํ๋ ํ์ผ.
// ํ์ํ js ํ์ผ์ ๊ฐ์ ธ์์ ์ฌ์ฉ.
const ๊ฐ์ธํ๋กํ = require('./two');
console.log(๊ฐ์ธํ๋กํ.์ด๋ฆ);
console.log(๊ฐ์ธํ๋กํ.๋์ด); //100
๊ฐ์ธํ๋กํ.ํ์ด๋๋จน์();
๊ฐ์ธํ๋กํ.ํ์ด๋๋จน์();
console.log(๊ฐ์ธํ๋กํ.๋์ด); //100 - export ํ๋ ์์ ์ ๋์ด๊ฐ ์ฐํ๋ ๊ฒ.
console.log(๊ฐ์ธํ๋กํ.๊ฐ๊ฐ์ ธ์ค๊ธฐ()); // 102
// ๋ชจ๋
let ์ด๋ฆ = 'ddosang';
let ๋์ด = 100;
function ํ์ด๋๋จน์() {
๋์ด += 1;
}
function ๊ฐ๊ฐ์ ธ์ค๊ธฐ() {
return ๋์ด;
}
module.exports.์ด๋ฆ = ์ด๋ฆ;
module.exports.๋์ด = ๋์ด;
module.exports.ํ์ด๋๋จน์ = ํ์ด๋๋จน์;
module.exports.๊ฐ๊ฐ์ ธ์ค๊ธฐ = ๊ฐ๊ฐ์ ธ์ค๊ธฐ;
// ๊ฐ๋จํ ์๋ฒ ๋ง๋ค๊ธฐ!
const http = require('http');
const fs = require('fs');
// ์คํํ๋ฉด ์๋์ ์ผ๋ก main, favicon์ ๋ํ request๊ฐ 2 ๋ฒ ๋ ์๊ฐ.
const app = http.createServer(function(request, response) {
let url = request.url;
console.log(`request : ${request}`);
console.log(`request : ${request.url}`);
if (request.url == '/') {
// ๋ฉ์ธํ์ด์ง ๋ณด์ฌ์ฃผ๊ธฐ
url = '/index.html';
} else if (request.url == '/blog') {
// ๋ธ๋ก๊ทธํ์ด์ง ๋ณด์ฌ์ฃผ๊ธฐ
url = '/blog.html';
} else if (request.url == '/favicon.ico') {
// favicon ์ ๋ชป์ฐพ์ผ๋ฉด...
// ๊ทผ๋ฐ ํดํน์ ๋๋นํด์ ๋ชป์ฐพ์๋ 200์ ๋ด๋ณด๋ด๊ธฐ๋ ํ๋ค.
return response.writeHead(404);
} else {
response.writeHead(404);
response.end('Not found');
}
response.writeHead(200);
// ์๋ต์ด ๋๋ ๋ ํด๋น ํ์ด์ง๋ฅผ ์ด์ด์ค.
response.end(fs.readFileSync(__dirname + url));
});
// 3000๋ฒ ํฌํธ์์ ๋์ํ๊ฒ ํ๋ค.
app.listen(3000);
npm init --yes
# package.json ์ด ์์ฑ๋จ.
๋ณดํต ๋ชจ๋์ ์ค์นํ๊ณ ๋์ ์ง์ฐ๊ณ , package.json, app.js๋ง ์
๋ก๋ํ๋ค.
npm์ ์ฑ์คํ ์ด ๊ฐ์ ๊ฒ์ด๋ผ๊ณ ๋ณด๋ฉด ๋๋ค.
// main
import * as ๊ฐ์ธํ๋กํ from './two.js'; // ์ ์ฒด ๋ค ๊ฐ์ ธ์ฌ ๋.
// import {์ด๋ฆ, ๋์ด, ํ์ด๋๋จน์, ๊ฐ๊ฐ์ ธ์ค๊ธฐ} from './two.js';
console.log(์ด๋ฆ);
console.log(๋์ด); //100
๊ฐ์ธํ๋กํ.ํ์ด๋๋จน์();
๊ฐ์ธํ๋กํ.ํ์ด๋๋จน์();
console.log(๋์ด); //102 - export๋ก ํ๋๊น ์ ๋๋ก ์ถ๋ ฅ!!!
console.log(๊ฐ๊ฐ์ ธ์ค๊ธฐ()); // 102
// module
export let ์ด๋ฆ = "ddosang";
export let ๋์ด = 10;
export function ํ์ด๋๋จน์() {
๋์ด += 1;
}
export function ๊ฐ๊ฐ์ ธ์ค๊ธฐ() {
return ๋์ด;
}
// ๊ณต์๋ฌธ์ ๋งํฌ : https://nodejs.org/dist/latest-v14.x/docs/api/path.html
const path = require('path');
console.log(`๊ตฌ๋ถ์ : ${path.sep}`);
console.log(`๋๋ ํ ๋ฆฌ : ${path.dirname(__filename)}`);
console.log(`ํ์ผ์ด๋ฆ : ${path.basename(__filename)}`);
console.log(`ํ์ฅ์ : ${path.extname(__filename)}`);
console.log(__filename);
console.log(__dirname);
console.table(path.parse(__filename));
console.log(path.join(__dirname, 'source')); // ๊ฒฝ๋ก์ ํด๋๋ ํ์ผ ์ถ๊ฐํ ๋ + ๊ฐ์๊ฑฐ ์ฐ์ง ๋ง๊ณ path.join์ผ๋ก ํฉ์น์.
// ๊ณต์๋ฌธ์ ๋งํฌ : https://nodejs.org/dist/latest-v14.x/docs/api/fs.html
const fs = require('fs');
// rename, readFile, writeFile, appendFile, copyFile, mkdir
// 1. rename
let ๋ณ์ = 'ddosang' // ์ฌ์ฉ์ ์ด๋ฆ
let ๋ ์ง = new Date()
fs.rename('./test.txt', `./${๋ณ์}${๋ ์ง.getMilliseconds()}.txt`, (err) => {
console.log(err); // ์ฌ์ฉ์๊ฐ ์ฌ๋ฆฐ ์ด๋ฏธ์ง๋ฅผ ์ด๋ค ์ด๋ฆ์ผ๋ก ์ฌ๋ฆด์ง์ ๋ํ ๊ณ ๋ ค๋ ๋ฏธ๋ฆฌ ํด์ผํ๋ค.
})
// 2. readFile
fs.readFile('./test.txt', 'utf8', (err, data) => {
console.log(err);
console.log(data);
})
// 3. readDirectory
fs.readdir('./', (err, data) => {
console.log(err);
console.log(data);
})
// 4. writeFile
fs.writeFile('./test2.txt', 'hello world 2', (err)=>{
console.log(err)
});
// 5. appendFile
fs.appendFile('./test2.txt', 'hello world 3', (err)=>{
console.log(err)
});
// 6. copyFile
fs.copyFile('./test2.txt', './test3.txt', (err)=>{
console.log(err)
});
// 6
fs.mkdir('./yoyo', (err)=>{
console.log(err)
});
Username, Identifier, job
booker, 129012, programmer
grey, 072070, coder
johnson, 814081, data scientist
jenkins, 469346, front-end developer
smith, 795079, back-end developer
Username : booker
Identifier : 129012
Job : promgrammer
// ๋ด๊ฐ ์ง ์ฝ๋ - ๋๋ readFile ์์์ ์ฝ๋ฐฑ์ ์จ์ ๊ทธ๋ฐ ๋ฌธ์ ๋ ์์์ง๋ง readFile์ ๋น๋๊ธฐ๋ผ์ ๋ฐ์์ ์์ฐจ์ ์ผ๋ก ์ฌ์ฉํ๋ฉด ํ์ผ์ ์ฝ์ง ์์๋๋ฐ ๋ค์๊ฒ์ด ์คํ๋ผ์ ์ค๋ฅ๊ฐ ๋๊ธฐ๋ ํ๋ค. readFileSync๋ Promise๋ฅผ ์ฌ์ฉํ์.
const fs = require('fs');
fs.readFile('./username.csv', 'utf8', (err, data) => {
// \n์ผ๋ก ํ๋ฒ ์๋ฅด๊ณ ๊ทธ๊ฑธ ', '๋ก ํ๋ฒ ์๋ผ์ 2์ฐจ์ array๋ก.
const dataArr = data.split('\n').map(item => item.split(', '));
// ์ฌ๋ ์ (arr ํ)๋งํผ ๋๋ฉด์
for(let i = 1; i < dataArr.length; i++){
// username ์ผ๋ก ํด๋ ์์ฑ.
fs.mkdir(`./${dataArr[i][0]}`, (err)=>{
console.log(err)
});
// ์ฌ๋ ์ ๋ณด ๊ฐ์(arr ์ด)๋งํผ ๋๋ฉด์
for (let j = 0; j < dataArr[0].length; j++) {
// file์ ์ ๋ณด ๋ฃ๊ธฐ!
fs.appendFile(`./${dataArr[i][0]}/userinfo.txt`, `${dataArr[0][j]} : ${dataArr[i][j]}\n`, (err)=>{
console.log(err)
});
// console.log(`${dataArr[0][j]} : ${dataArr[i][j]}`);
}
}
});
// ๋ค๋ฅธ ๋ถ ์ฝ๋๋ฅผ ๋ณด๊ณ ๋ณด์ํ ์ฝ๋!!
fs.readFile('./username.csv', 'utf8', (err, data) => {
const splitedData = data.split('\n').map(info => info.split(', '));
const columnTitle = splitedData[0];
splitedData.slice(1)
.forEach((personData) => {
username = personData[0];
fs.mkdir(`./${username}`, (err) => {});
for (i in personData) {
console.log(i);
fs.appendFile(`./${username}/userinfo.txt` ,`${columnTitle[i]} : ${personData[i]}\n`, (err) => {});
}
});
});
\n : LineFeed
\r : Carriage Return
๋ฆฌ๋
์ค์์๋ \r์ด ์๋จนํ์ ์์์ \n์ผ๋ก ๋ฐ๊พผ๋ค.
๋ split ์ฐ๋ฉด ๋๋๊ฑฐ ์ ๊ทํํ์์ธ๊ฐ..? ํ๊ณ ํค๋งธ๋ค.. split!!! split!!!!
http ๋ชจ๋์ ๊ฐ์ง๊ณ ์์ GET, POST ๋ฐฉ์์ผ๋ก ๋ฆฌํ์คํธ๋ฅผ ๋ ๋ ธ์ ๋ ๋ฐ์ดํฐ๊ฐ ์ด๋ ๋ค์ด๊ฐ๋์ง ์ฐ์ด๋ณด๋ ์์ ์ ํ๋ค. ์์ง response ์ฒ๋ฆฌํ๋ ๊ฒ์ ๊ทธ๋ฅ ๋ญ ํ๋๊ฑฐ๊ฒ ๋ค ๋ผ๋ ๋๋๋ง ์๊ณ ์๋ฒฝ ์ดํด๋ ์๋๋ค..!
// ๋ชจ๋ ํฌํจ.
const http = require('http');
const fs = require('fs');
const querystring = require('querystring');
const server = http.createServer(function(req, res){
console.log('server๊ฐ ๊ตฌ๋์ค์
๋๋ค.');
// get๊ณผ post ๋ ๋ค ํ
์คํธ
if(req.method == 'GET') {
fs.readFile('./test.html', 'utf8', (err, data) => {
console.log(err);
//writeHead๋์ setHeader๋ฅผ ์ฌ์ฉํ์ฌ type์ ๋ณด๋ผ ์ ์์
res.writeHead(200, {'Content-Type':'text/html'});
res.write(data);
res.end();
})
} else if(req.method =='POST') {
req.on('data', function(chunk) {
console.log(chunk.toString());
let data = querystring.parse(chunk.toString());
console.log(data);
res.writeHead(200, {'Content-Type':'text/html'});
res.write(`id : ${data.id_value}, pw : ${data.pw_value}`);
res.end();
});
}
});
server.listen(8080);
amend ๊ฐ ์ฝ๊ฐ ๊ฐ์ด ์์ค๋๋ฐ.. ๊ธฐ๊ป ์ปค๋ฐํด๋จ๋๋ ์คํ๊ฐ ์์ด์ ๋ค์ ์ปค๋ฐํด์ผ ํ ๋ ์ฐ๋ฉด ๋ ๊ฒ ๊ฐ๋ค. ๊ทผ๋ฐ ๋ ๋ค๋ฅธ ์ฌ๋์ด๋ ๊ณต๋ ์์ ์ค์ด๋ฉด ์์ฐ๋๊ฒ ๋ง๋ค๊ณ ํด์ ๋ ์..? ํ๋ ์ค์ด๋ค. ํผ์ ์ธ ๋ ์ ์จ๋ด์ผ๊ฒ ๋ค. pull request ๊ฐ ๋ญ์ง ํญ์ ๊ถ๊ธํ์์ง๋ง ์ง์ง ํ๋ก์ ํธ๋ฅผ ํ๋๊ฒ ์๋๋ผ์ ์ผ๋จ ๋ฏธ๋ค์๋๋ฐ ์ด๊ฑธ ๋ฐฐ์ด๊ฒ ๋๋ฌด ์ข๋ค.
์ด ์ผ๋จ...! ์ฌ๋ฐ์๋ค! ๋ญ๊ฐ ์งง์ ์ฝ๋๋ก ์ค์ ์๋น์ค์ฒ๋ผ url์ ๊ฒฝ๋ก ๋ถ์ผ๋ฉด์ ํ์ด์ง๋ฅผ ๋์ด๊ฐ ์ ์๋๊ฒ ์ ๊ธฐํ๋ค.
ํ๋ก์ ํธ ํ ๋ ๋ฐ์ดํฐ ํ์ ๋ณด๋ฌ ๋ฐฑ์๋ ์ฝ๋ ํ์ธํ๋ฌ ์ ๊น์ ๊น ๋ค์ด๊ฐ์๋๋ฐ ์๋ฌด๋ฆฌ ์๊ฐํด๋ ํ๋ก ํธ๋จ๋ณด๋ค ์ฝ๋๊ฐ ์งง์๋ณด์ด๋๋ฐ ํ์๋ค์ด ํญ์ ์ค๋ฅ ๋๋ฌธ์ ํ๋ค์ดํ๊ณ ์์ด์ ์ด๋ค ์ค๋ฅ๊ฐ ๋๋๊ฑธ๊น? ๋ผ๋ ์๊ฐ์ ํ์๋๋ฐ ์ด์ ์ดํด๊ฐ ๊ฐ๋ค!