node.js
JS Warm Up
- JavaScript platform like JVM of Java
- JSON
์ค์น
node.js ๊ฐ์
Node.jsยฎ is a platform
built on Chrome's JavaScript runtime
for easily building fast, scalable network applications.
Node.js uses an event-driven, non-blocking I/O model
that makes it lightweight and efficient, perfect
for data-intensive real-time applications
that run across distributed devices.
ํน์ง
- JavaScript ๊ธฐ๋ฐ ํ๋ซํผ
- Server-side
- Command Tool
- Desktop Application
์ฅ์
- ์ฌ์ด ์์
- ์์ ์ ์ธ ์๋น์ค w/ pm2
- ๋ชจ๋ 2,663,939 total packages(2024/01/27); https://modulecounts.com
- ์ฑ๋ฅ ๊ฐ์
- linkedin ์ฌ๋ก
- ์๋ฒ ๊ฐ์ถ Ruby + Mongrel 30๋ -> node.js 3๋
- Frontend, Backend ๊ฐ๋ฐ์ ์ํต ์ํ
- ์ก๋คํ ์์
๊ฐ์ํด์ ๋ก์ง์ ์ง์ค
- paypal ์ฌ๋ก
- Java๋ฅผ Node.js๋ก ๋์น
- ๋ ์ ์ ์ธ์์ผ๋ก ๊ฐ๋ฐ 2๋ฐฐ ์ ๋ ๋นจ๋ผ์ง
- ์ฝ๋๋ 33%, ํ์ผ 40% ๊ฐ์
- ์ฑ๋ฅ ๊ฐ์ ๋จ (15-user: 11.3 vs 21.6 pages/sec)
- C/C++ Addon
๋จ์
- No Silver Bullet
- CPU๊ณผ๋ค ์ฌ์ฉ์ ์ด์
- ์์ธ์ฒ๋ฆฌ ์ค์ํ๋ฉด ์๋ฒ ๋ค์ด (pm2๋ก ์๋ ์ฌ์์ ๊ฐ๋ฅ)
- Callback Hell (Async๋ก ์ปค๋ฒ ๊ฐ๋ฅ)
๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ
- ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฒฝ์ฐ ๋น๋๊ธฐ ์ฝ๋ ํจํด์ด ๋ค๋ฐ์ฌ
- ๊ธฐ์กด ์ฝ๋์ ๋ฌ๋ฆฌ ์ฝ๋ฐฑ ํจ์๋ฅผ ํจ๊ป ๋๊ฒจ์ฃผ๋ ํํ
- ์ด๋ฒคํธ ํธ๋ค๋ง์ ๋ง์ด ์ฐ์ด๋ ๊ธฐ๋ฒ
- ์ด๋ฒคํธ ๋ฃจํ ์ฌ์ฉ์ผ๋ก ์ฑ๊ธ์ฐ๋ ๋ ๋์
blocking, non-blocking
- Input/Output
- IO latency
- L1: 3 cycles
- L2: 14 cycles
- RAM: 250 cycles
- DISK: 41,000,000 cycles
- NETWORK: 240,000,000 cycles
from: https://www.dropbox.com/s/o9g4m7tug3yt1xx/jsconf2009-nodejs.pdf?dl=0
var result = db.query("select * from T");
// use result
db.query("select * from T",
function(result) {
// use result
}
);
์ด๋ฒคํธ ๋ฃจํ
from blog.udemy.com/learn-node-js/
- ์ด๋ฒคํธ ๋ฃจํ ์ฌ์ฉ์ผ๋ก ์ฑ๊ธ์ฐ๋ ๋ ๋์
- ๊ธฐ์กด ์๋ฒ๋ ์ฐ๋ ๋ ๊ธฐ๋ฐ
- ์ํ์น ๋ฑ์ ์ปค๋ฅ์
์ฆ๊ฐ์ ๋ฐ๋ผ์ ๋ฉ๋ชจ๋ฆฌ ์ฆ๊ฐ
- nginx๋ ์ด๋ฒคํธ ๋ฃจํ ๋ฐฉ์
์๋ฒ์ฌ์ด๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ๋ฐํ๊ฒฝ ์ค์น
๋ชจ๋ ๋ง๋ค๊ณ ์ฐธ์กฐํ๊ธฐ
- commonjs
const path = require('path')
ES6 setting
package.json
"type": "module"
import path from path
npm ์ ํตํ ํ์ฅ
socket.io ๋ชจ๋
์คํ
npm i
node server.js
expressjs ์น ํ๋ ์์ํฌ
- http://expressjs.com/
- npm install -g express-generator
- express myapp
- node.js์ ๊ธฐ๋ณธ API์ธ http๋ฅผ ํ์ฅ
- ๊ธฐ๋ณธ http
express.js
- with package.json
npm init
npm install express --save
- create
exp.js
file
express-generator
npm install express-generator -g
- express.js์ scafold ์ฝ๋ ์์ฑ
express myweb
cd myweb && npm install
- run
npm start
- or
node bin/www
- or
DEBUG=myweb:* node bin/www
basic routing
app.METHOD(PATH, HANDLER)
- app is an instance of express.
- METHOD is an HTTP request method, in lowercase.
- PATH is a path on the server.
- HANDLER is the function executed when the route is matched.
get
curl 'localhost:3000'
curl -XGET 'localhost:3000'
post
curl -XPOST 'localhost:3000'
put
curl -XPUT 'localhost:3000/user'
delete
curl -XDELETE 'localhost:3000/user'
Routing
methods
get, post, put, head, delete, options, trace,
copy, lock, mkcol, move, purge, propfind, proppatch,
unlock, report, mkactivity, checkout, merge, m-search,
notify, subscribe, unsubscribe, patch, search, connect.
Route Parameters
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
app.get('/users/:userId/books/:bookId', function (req, res) {
res.send(req.params)
})
MairaDB ์ค์น
MongoDB + node.js
node cluster ์์ํ๊ธฐ
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
node.js clustering with PM2
JavaScript Code Quality
๋๋ฒ๊น
๊ฐ์
- debugging์์ ์์์ผ ํ ์ฉ์ด๋ค
- breakpoint : ์ค๋จ์ ์
๋๋ค. ์คํ ๋ชจ๋๊ฐ ์๋ ๋๋ฒ๊ทธ ๋ชจ๋์์ ํ๋ก๊ทธ๋จ์ ์ค์งํ๊ฒ ๋๋ ์ง์ ์ ํ์์
๋๋ค. ๋ณดํต ide์์ ์์ค ๋ผ์ธ ๋งจ ์ ์ฌ๋ฐฑ์ (๋๋ธ)ํด๋ฆญํ๋ฉด ์๊น๋๋ค. ๋ค์ (๋๋ธ)ํด๋ฆญํ๋ฉด ์์ด์ง๋๋ค. resume์ ์คํํ๋ฉด ๋ค์ ์ค๋จ์ ์ ๋ง๋ ๋๊น์ง ์คํ๋ฉ๋๋ค.
- step over : ํ ์ค์ ์คํํฉ๋๋ค. ํจ์๊ฐ ์์ด๋ ์คํ ํ ๋ค์์ผ๋ก ๋์ด๊ฐ๋๋ค.
- step into : ํฌ์ปค์ค๋ ๋ผ์ธ์ ์๋ ํจ์ ๋ด๋ถ๋ก ๋ค์ด๊ฐ๋๋ค.
- step out : ํ์ฌ ํจ์๋ฅผ ๋๊น์ง ์คํ์ํค๊ณ , ํธ์ถ์ํจ ๊ณณ์ผ๋ก ๋๋์ ๊ฐ๋๋ค.
- resume : ๋๋ฒ๊ทธ๋ก ํ ์ค ํ ์ค ์คํ์ํค๋ ํธ๋ ์ด์ค ๋ชจ๋๋ฅผ ๊ทธ๋ง๋๊ณ ๋ค์ ๋ธ๋ ์ดํฌํฌ์ธํธ๋ฅผ ๋ง๋ ๋๊น์ง ์คํํฉ๋๋ค.
node.js debug
node debug myscript.js
x = 5;
setTimeout(() => {
debugger;
console.log('world');
}, 1000);
console.log('hello');
email
- node-emailer
- gmail smtp ์ค์ ํ์
- OAuth2
Test Frameworks
QUnit
Testing Essentials
UI ํ
์คํธ
์๋ฒ ๋ชจ๋ํฐ๋ง
$ cd ~/dev/mongodb/bin
$ mkdir -p ~/data/db
$ ./mongod --dbpath=/Users/kenu/data/db
$ cd ~/dev/mongodb/bin
$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use uptime
switched to db uptime
> db.addUser('uptime', 'okpass');
{
"user" : "uptime",
"readOnly" : false,
"pwd" : "fdc9e10c8f90fac0c9fe786f28cc04f4",
"_id" : ObjectId("5577b97a62555c0332f63e6f")
}
> exit
$ git clone git://github.com/fzaninotto/uptime.git
$ cd uptime
$ npm install
์์
- ๊ฐ์, ์ค์น https://www.youtube.com/watch?v=Z8cOppJwOeU
- socket.io, express.js https://www.youtube.com/watch?v=YWaoLdoqSWE
- express.js basic routing https://www.youtube.com/watch?v=KOtEZ0j0cic
- express.js api https://www.youtube.com/watch?v=-nHp0D4n4Do
- express.js resources https://www.youtube.com/watch?v=4bIIX07vN-8
- mariadb ์ฐ๊ฒฐ(Windows) https://www.youtube.com/watch?v=VTBACySodEc
- node cluster https://www.youtube.com/watch?v=2v7oRhit8lQ
- node debugging https://www.youtube.com/watch?v=TaMYKdcBIGA
- Code Quality https://www.youtube.com/watch?v=1931C8MAvdM
- jade 2 ejs template https://www.youtube.com/watch?v=IeOoN1LZ4f0
- Express + MariaDB(mysql) Web App https://www.youtube.com/watch?v=UocHh8604Lc
- Async, Nested SQL https://www.youtube.com/watch?v=PJ7fQnDLmWg
- email
related
ref