2023-02-26 01:30
This commit is contained in:
1
getting-started/.gitignore
vendored
Normal file
1
getting-started/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
9
getting-started/.vscode/settings.json
vendored
Normal file
9
getting-started/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"editor.fontFamily": "'Source Code Pro','Droid Sans Mono', 'monospace', monospace, 'Droid Sans Fallback'",
|
||||
"telemetry.enableCrashReporter": false,
|
||||
"telemetry.enableTelemetry": false,
|
||||
"files.autoGuessEncoding": true,
|
||||
"files.autoSave": "onFocusChange",
|
||||
"files.insertFinalNewline": true,
|
||||
"files.trimTrailingWhitespace": true
|
||||
}
|
||||
26
getting-started/01_console.js
Normal file
26
getting-started/01_console.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
https://nodejs.dev/
|
||||
*/
|
||||
/**
|
||||
* 콘솔 출력
|
||||
*/
|
||||
console.log("Hello,");
|
||||
setTimeout(() => {
|
||||
console.log(" world.");
|
||||
|
||||
}, 1000);
|
||||
|
||||
const x = 100, y = 120;
|
||||
console.log(x, y);
|
||||
|
||||
const age = 24, name = "Charlie";
|
||||
console.log("%s's age is %d.", name, age);
|
||||
|
||||
// 스택 트레이스 출력
|
||||
console.trace();
|
||||
|
||||
// 시간 측정
|
||||
console.time("myStopwatch");
|
||||
// do something ..
|
||||
console.timeEnd("myStopwatch");
|
||||
|
||||
17
getting-started/02_process.js
Normal file
17
getting-started/02_process.js
Normal file
@@ -0,0 +1,17 @@
|
||||
// 프로세스 종료 이벤트 수신
|
||||
process.on('SIGTERM', function () {
|
||||
console.log("bye~");
|
||||
});
|
||||
/* process.on("SIGKILL", function () {
|
||||
console.log("앱 죽어.");
|
||||
process.exitCode = 1;
|
||||
}); */
|
||||
|
||||
// 환경 변수
|
||||
console.log(process.env.PATH);
|
||||
console.log(process.env.NODE_ENV); // development
|
||||
|
||||
// 프로그램 매개변수
|
||||
process.argv.slice(2).forEach((val, index) => {
|
||||
console.log(`${index}: ${val}`)
|
||||
})
|
||||
10
getting-started/03_progressbar.js
Normal file
10
getting-started/03_progressbar.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const ProgressBar = require("progress");
|
||||
//import { ProgressBar } from "progress";
|
||||
|
||||
const bar = new ProgressBar(':percent', { total: 100 });
|
||||
const timer = setInterval(() => {
|
||||
bar.tick();
|
||||
if (bar.complete) {
|
||||
clearInterval(timer);
|
||||
}
|
||||
}, 10);
|
||||
12
getting-started/04_readline.js
Normal file
12
getting-started/04_readline.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const readline = require('readline').createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
readline.question("Choose?", choice => {
|
||||
console.log(`Your choice is ${choice}.`);
|
||||
readline.close();
|
||||
});
|
||||
|
||||
|
||||
|
||||
7
getting-started/05_readline_sync.js
Normal file
7
getting-started/05_readline_sync.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const readline = require('readline-sync');
|
||||
|
||||
let id = readline.question("ID?");
|
||||
let pw = readline.question("Password?", { hideEchoBack: true });
|
||||
|
||||
console.log(`Your ID is ${id}, and password is ${pw}.`);
|
||||
|
||||
6
getting-started/06_another_module.js
Normal file
6
getting-started/06_another_module.js
Normal file
@@ -0,0 +1,6 @@
|
||||
const person = {
|
||||
age: 34,
|
||||
name: "Steve"
|
||||
};
|
||||
|
||||
module.exports = person;
|
||||
3
getting-started/06_module.js
Normal file
3
getting-started/06_module.js
Normal file
@@ -0,0 +1,3 @@
|
||||
const person = require('./06_another_module');
|
||||
|
||||
console.log(person);
|
||||
24
getting-started/07_timer.js
Normal file
24
getting-started/07_timer.js
Normal file
@@ -0,0 +1,24 @@
|
||||
let fun0 = function () {
|
||||
console.log("Hello");
|
||||
};
|
||||
|
||||
let fun2 = function (param1, param2) {
|
||||
console.log(param1, param2);
|
||||
};
|
||||
|
||||
setTimeout(fun0, 50);
|
||||
setTimeout(fun2, 100, 1, 2);
|
||||
|
||||
const timerId = setTimeout(() => { console.log("Hi"); }, 2000);
|
||||
clearTimeout(timerId);
|
||||
|
||||
/**
|
||||
*/
|
||||
const intervalId = setInterval(fun0, 100);
|
||||
setTimeout(() => { clearInterval(intervalId) }, 1000);
|
||||
|
||||
/**
|
||||
*/
|
||||
setImmediate(() => { });
|
||||
|
||||
process.nextTick(() => { });
|
||||
21
getting-started/08_promise.js
Normal file
21
getting-started/08_promise.js
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
let fun1 = async function () {
|
||||
let someJobIsDone = true;
|
||||
let i = 0;
|
||||
while (i < 100000000) {
|
||||
i++;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if (someJobIsDone) {
|
||||
resolve('ok');
|
||||
} else {
|
||||
reject('bad');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
fun1()
|
||||
.then(msg => console.log(msg))
|
||||
.catch(err => console.error(err));
|
||||
console.log("Hi");
|
||||
11
getting-started/09_event_emitter.js
Normal file
11
getting-started/09_event_emitter.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const Events = require("events");
|
||||
const eventEmitter = new Events();
|
||||
|
||||
eventEmitter.on('start', () => { console.log("onStart") });
|
||||
|
||||
eventEmitter.emit('start');
|
||||
|
||||
// one time listener
|
||||
eventEmitter.once('start', () => { console.log("onStart") });
|
||||
|
||||
eventEmitter.off('start');
|
||||
23
getting-started/10_http_server.js
Normal file
23
getting-started/10_http_server.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const http = require('http');
|
||||
const port = 19090;
|
||||
|
||||
const server = http.createServer((req, resp) => {
|
||||
let data = [];
|
||||
req.on('data', (chunk) => {
|
||||
data.push(chunk);
|
||||
});
|
||||
req.on('end', () => {
|
||||
resp.statusCode = 200;
|
||||
resp.setHeader('Content-Type', 'text/html');
|
||||
resp.end(`<p>Hello, <b>${JSON.parse(data).message}</b>!</p>`);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
server.listen(port, () => {
|
||||
console.log('listening on ' + port);
|
||||
});
|
||||
process.on('SIGINT', () => {
|
||||
console.log('closing.. ');
|
||||
server.close();
|
||||
});
|
||||
22
getting-started/11_https_request.js
Normal file
22
getting-started/11_https_request.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const https = require('https');
|
||||
const options = {
|
||||
hostname: 'www.elex-project.com',
|
||||
port: 443,
|
||||
path: '/',
|
||||
method: 'GET',
|
||||
headers: { 'User-Agent': 'Elex Node' }
|
||||
};
|
||||
|
||||
const request = https.request(options, (response) => {
|
||||
console.log(`httpStatus: ${response.statusCode}`);
|
||||
|
||||
response.on('data', (data) => {
|
||||
process.stdout.write(data);
|
||||
});
|
||||
});
|
||||
|
||||
request.on('error', (error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
request.end();
|
||||
33
getting-started/12_https_request_post.js
Normal file
33
getting-started/12_https_request_post.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const https = require('https');
|
||||
const body = JSON.stringify({
|
||||
id: 1234,
|
||||
message: 'Hello'
|
||||
});
|
||||
const options = {
|
||||
hostname: 'www.elex-project.com',
|
||||
port: 443,
|
||||
path: '/',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'User-Agent': 'Elex Node',
|
||||
'Content-Type': `application/json`,
|
||||
'Content-Length': body.length
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
const request = https.request(options, (response) => {
|
||||
console.log(`httpStatus: ${response.statusCode}`);
|
||||
|
||||
response.on('data', (data) => {
|
||||
process.stdout.write(data);
|
||||
});
|
||||
});
|
||||
|
||||
request.on('error', (error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
request.write(body);
|
||||
request.end();
|
||||
15
getting-started/13_https_request_with_axios.js
Normal file
15
getting-started/13_https_request_with_axios.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const axios = require('axios').create({
|
||||
headers: { 'User-Agent': 'Elex Node' }
|
||||
});
|
||||
|
||||
axios
|
||||
.post('https://www.elex-project.com/', {
|
||||
id: 1234, message: 'Hello~'
|
||||
})
|
||||
.then((response) => {
|
||||
console.log(`httpStatus: ${response.statusCode}`);
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
18
getting-started/14_https_request_with_request.js
Normal file
18
getting-started/14_https_request_with_request.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const request = require('request');
|
||||
|
||||
request.post(
|
||||
'http://localhost:19090',
|
||||
{
|
||||
headers: { 'User-Agent': 'Elex Node' },
|
||||
json: { id: 1234, message: 'Charlie' }
|
||||
},
|
||||
(error, response, body) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
} else {
|
||||
console.log(`httpStatus: ${response.statusCode}`);
|
||||
console.log(body);
|
||||
}
|
||||
}
|
||||
|
||||
);
|
||||
34
getting-started/15_file.js
Normal file
34
getting-started/15_file.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
get file descriptor async
|
||||
*/
|
||||
fs.open('sample.txt', 'r', (error, fd) => {
|
||||
console.log(fd);
|
||||
});
|
||||
|
||||
/**
|
||||
get file descriptor sync
|
||||
*/
|
||||
const fd = fs.openSync('sample.txt', 'r');
|
||||
console.log(fd);
|
||||
|
||||
/**
|
||||
get file stat async
|
||||
*/
|
||||
fs.stat('sample.txt', (error, stat) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
} else {
|
||||
console.log(stat);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
get file stat sync
|
||||
*/
|
||||
const stat = fs.statSync('sample.txt');
|
||||
console.log(stat);
|
||||
console.log(stat.isDirectory());
|
||||
console.log(stat.isSymbolicLink());
|
||||
console.log(stat.size);
|
||||
26
getting-started/16_path.js
Normal file
26
getting-started/16_path.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const path = require('path');
|
||||
|
||||
const p = '/var/sample.txt';
|
||||
|
||||
// containing dir
|
||||
console.log(path.dirname(p));
|
||||
// filename
|
||||
console.log(path.basename(p));
|
||||
// ext
|
||||
console.log(path.extname(p));
|
||||
// file name without ext
|
||||
console.log(path.basename(p, path.extname(p)));
|
||||
|
||||
// build abs path
|
||||
const userName = 'steve';
|
||||
console.log(
|
||||
path.resolve('/', 'users', userName, 'notes.txt')
|
||||
);
|
||||
// full path with cwd
|
||||
console.log(
|
||||
path.resolve('sample.txt')
|
||||
);
|
||||
// normalize
|
||||
console.log(
|
||||
path.resolve('/users/steve/..//sample.txt')
|
||||
);
|
||||
41
getting-started/17_file_io.js
Normal file
41
getting-started/17_file_io.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const fs = require('fs');
|
||||
|
||||
// read
|
||||
fs.readFile('sample.txt', (error, data) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
} else {
|
||||
console.log(data);
|
||||
}
|
||||
});
|
||||
|
||||
// read sync
|
||||
try {
|
||||
const data = fs.readFileSync('sample.txt');
|
||||
console.log(data);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// write
|
||||
const content = 'Test Text';
|
||||
fs.writeFile('test.txt', content, (error) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// write sync, append mode
|
||||
try {
|
||||
fs.writeFileSync('test.txt', content, { flag: 'a+' });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// another append mode
|
||||
fs.appendFile('test.txt', content, (error) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
41
getting-started/18_folder.js
Normal file
41
getting-started/18_folder.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const folder = '/home/elex/nodeTest';
|
||||
|
||||
|
||||
try {
|
||||
if (!fs.existsSync(folder)) {
|
||||
fs.mkdirSync(folder);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
|
||||
let ls = fs.readdirSync(folder)
|
||||
.map((filename) => {
|
||||
return path.join(folder, filename);
|
||||
});
|
||||
console.log(ls);
|
||||
|
||||
// with a filter
|
||||
let ls2 = fs.readdirSync(folder)
|
||||
.map((filename) => {
|
||||
return path.join(folder, filename);
|
||||
}).filter((filename) => {
|
||||
return fs.lstatSync(filename).isFile();
|
||||
});
|
||||
console.log(ls2);
|
||||
|
||||
|
||||
// a folder exists and has a permission
|
||||
fs.accessSync(folder)
|
||||
|
||||
// rename
|
||||
fs.rename('/home/elex/nodeTest/새 텍스트 파일', '/home/elex/nodeTest/test.txt', (error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
// delete a folder
|
||||
fs.rmdir(folder);
|
||||
18
getting-started/19_os.js
Normal file
18
getting-started/19_os.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const os = require('os');
|
||||
|
||||
console.log(os.arch());
|
||||
console.log(os.cpus());
|
||||
console.log(os.endianness());
|
||||
|
||||
console.log(os.freemem());
|
||||
console.log(os.homedir());
|
||||
console.log(os.hostname());
|
||||
console.log(os.loadavg());
|
||||
console.log(os.networkInterfaces());
|
||||
console.log(os.platform());
|
||||
console.log(os.release());
|
||||
console.log(os.tmpdir());
|
||||
console.log(os.totalmem());
|
||||
console.log(os.type());
|
||||
console.log(os.uptime());
|
||||
console.log(os.userInfo());
|
||||
23
getting-started/20_buffer.js
Normal file
23
getting-started/20_buffer.js
Normal file
@@ -0,0 +1,23 @@
|
||||
// 0으로 초기화됨.
|
||||
const buffer = Buffer.alloc(1024);
|
||||
|
||||
//or 초기화 없음
|
||||
// buffer = Buffer.allocUnsafe(1024);
|
||||
buffer.write('Hi.');
|
||||
console.log(buffer.toString());
|
||||
|
||||
const buff = Buffer.from('Hello');
|
||||
console.log(buff[0]);
|
||||
console.log(buff.length);
|
||||
console.log(buff.toString());
|
||||
for (const c of buff) {
|
||||
console.log(c);
|
||||
}
|
||||
|
||||
|
||||
// copy
|
||||
let buf2 = Buffer.alloc(2);
|
||||
buffer.copy(buf2, 0, 0, 2);
|
||||
|
||||
// slice
|
||||
console.log(buffer.slice(0, 2).toString());
|
||||
25
getting-started/21_stream.js
Normal file
25
getting-started/21_stream.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const fs = require('fs');
|
||||
const Stream = require('stream')
|
||||
|
||||
const readableStream = new Stream.Readable(
|
||||
{ read() { } }
|
||||
);
|
||||
const readableStream2 = fs.createReadStream('sample.txt');
|
||||
|
||||
const writableStream = new Stream.Writable(
|
||||
{
|
||||
write(chunk, encoding, next) {
|
||||
console.log(chunk.toString())
|
||||
next()
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
readableStream.pipe(writableStream);
|
||||
|
||||
readableStream.on('readable', () => {
|
||||
console.log(readableStream.read());
|
||||
});
|
||||
|
||||
writableStream.write('Hello...');
|
||||
writableStream.end();
|
||||
21
getting-started/package.json
Normal file
21
getting-started/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "getting-started",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"eslint": "npx eslint --init",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Elex<mysticzizone@gmail.com>",
|
||||
"license": "ISC",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"progress": "^2.0.3",
|
||||
"readline-sync": "^1.4.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"axios": "^0.19.2",
|
||||
"eslint": "^6.8.0"
|
||||
}
|
||||
}
|
||||
2
getting-started/sample.txt
Normal file
2
getting-started/sample.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Hello, Hahaha.
|
||||
Test Text
|
||||
1
getting-started/test.txt
Normal file
1
getting-started/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
Test TextTest Text
|
||||
Reference in New Issue
Block a user