Node.js๋?
: A runtime environment for executing Javascript(js) code
- ์ฃผ๋ก ๋ฐฑ์ค๋ ์๋น์ค ๊ตฌ์ถ์ ์ฌ์ฉ๋จ ex) API(Application Programming Interface)
- ์น์ฑ, ๋ชจ๋ฐ์ผ ์ฑ์ด ๋ฐฑ์ค๋ ์๋น์ค์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ฑฐ๋, ์ด๋ฉ์ผ์ ๋ณด๋ด๊ฑฐ๋, ํธ์ฌ ์๋์ ๋ณด๋ผ๋ ๋ฑ
- Non-blocking(๋ฉ์ถ์ง ์๊ณ ๊ธฐ๋ค๋ฆฌ๊ธฐ), Asynchronous(๋น๋๊ธฐ) ์ด๋ฏ๋ก ํ์ฅ์ฑ์ด ๋ฐ์ด๋๊ณ , ๋ฐ์ดํฐ ์ค์ฌ, ์ค์๊ฐ ์ฑ์ ๋ง๋ค์ ์๋ค.
Node๋ง์ ์ฅ์ ?
- ํ๋กํ ํ์ดํ๊ณผ ๋น ๋ฅธ ๊ฐ๋ฐ์ ์ ์ฉ
- ๋งค์ฐ ๋น ๋ฅด๊ณ ํ์ฅ๊ฐ๋ฅํจ, ์ฆ, ๋ ๊ฐ๊ฒฐํ ์ฝ๋, ํ์ผ๋ก ๋ ๋น ๋ฅธ ์๋น์ค ๊ตฌ์ถ ๊ฐ๋ฅ
- ์๋ฐ ์คํฌ๋ฆฝํธ๋ ์ด๋๋ ์ง ์์ (node.js๋ javascript ๊ธฐ๋ฐ)
- ๊น๋ํ๊ณ ์ผ๊ด์ฑ ์๋ ์ฝ๋ ๊ธฐ๋ฐ
- ์คํ ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํฌ๊ฒ ํ์ฑํ ๋์ด ์์
Node ๊ตฌ์กฐ
- ๊ธฐ์กด์๋ JS code๋ฅผ->JS Engine์์->Machine code ๋ก ๋ณํ,
js code๋ฅผ ๋ธ๋ผ์ฐ์ (ex) chrome) ์์์ ๋ฐ์ ์คํ์ ๋ชปํ์
- 2009, Byan Dahl - Js code๋ฅผ ๋ธ๋ผ์ฐ์ ๋ฐ์์ ์คํํ๋ค๋ ์์ด๋์ด
[Google v8 engine (faster js engine)] + [embedded c++ program(some additional module)]
=> Node.exe
- Javascript code์ runtime environment -> ๊ทธ๋ฌ๋ ๋ธ๋ผ์ฐ์ ์ ์๋ environment์ object์๋ ๋ค๋ฅธ object(๊ฐ์ฒด)๋ฅผ ๊ฐ๊ณ ์์
์๋ฅผ๋ค์ด,
์๋ฐ์คํฌ๋ฆฝํธ์์์ document.getElementById(‘’); -> document object๊ฐ ์๋
๋ ธ๋๋ fs.readFilew(), http.createServer()
-> fs (ํ์ผ ์์คํ ), http (listen for request (๋คํธ์ํฌ์์)) ๋ฑ๊ณผ ๊ฐ์ object๋ฅผ ๊ฐ๊ณ ์์
=> ํฌ๋กฌ, ๋ ธ๋ ๋ชจ๋ ๊ฐ์ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ๊ณต์ ํ๋๋ฐ ๊ฐ๊ฐ ๋ค๋ฅธ ๋ฐํ์ ํ๊ฒฝ์ ์ ๊ณตํ๋ค.
Node is NOT a programming language!
Node is NOT a framework!
It’s a runtime environment for executing JavaScript code
How Node Works?
<Non-bloking Asynchronous>
์์์ ์ ๊ฐ๋ฉด ์ ์์ด ํ ์ด๋ธ1์ ์ฃผ๋ฌธ๋ฐ๊ณ ํ ์ด๋ธ2๋ฅผ ์ฃผ๋ฌธ๋ฐ๋ ๋์ ์๋ฆฌ์ฌ๊ฐ ์๋ฆฌ๋ฅผ ํ๋ค -> ํ ์ด๋ธ1 ์ฃผ๋ฌธ๋ฐ๊ณ ๊ทธ ์๋ฆฌ๊ฐ ๋์ฌ๋๊น์ง ๊ธฐ๋ค๋ฆด ํ์๊ฐ ์์
์ ์-> Sigle Thread / ํ ์ด๋ธ -> Request ๋ผ๊ณ ์๊ฐ
<Blocking Synchronous์ ๋ฌธ์ ์ >
Thread๋ ํ์ ๋ ์์(2๊ฐ ์๋ค ์น์) -> request 1๊ฐ ๋ค์ด์ด -> thread 1์ด request 1(client)์ด ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ -> request 2๊ฐ ๋ค์ด์ด -> thread 2๊ฐ request 2๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ -> thread 3๊ฐ ์ค๋ฉด thread1, 2๊ฐ ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ ค์ผํจ
=> ์์ฒญ ๋ง์ client๋ผ๋ฉด? Thread ๋ถ์กฑ -> ๊ธฐ๋ค๋ ค์ผํจ or ํ๋์จ์ด๋ฅผ ์ถ๊ฐํด์ผํจ
๋ ธ๋ ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ํดํธ๋ก asynchronous(๋น๋๊ธฐ)์ด๊ธฐ ๋๋ฌธ์,
single thread๊ฐ ๋์์ request๋ฅผ handle ๊ฐ๋ฅ
- request 1์ด database๋ฅผ ์์ฒญํ๋ค๋ฉด database๊ฐ data๋ฅผ return ํ ๋๊น์ง ๊ธฐ๋ค๋ฆด ํ์x
- database ๊ฐ query๋ฅผ ์งํํ ๋ ๋์ request 2๋ฅผ handle
database ๊ฐ ๊ฒฐ๊ณผ๋ฅผ ์ค๋นํ๋ฉด -> event queue ๋ฉ์ธ์ง ๋ฐ์
(signle thread ๋ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์ด๋ฅผ ๋ณด๊ณ ์๋ค๊ฐ -> ๊ฒฐ๊ณผ๋ฅผ request 1์๊ฒ ์ค
- Node is ideal for I/O-intensive app
- ํ๋์จ์ด ์ถ๊ฐ ์์ด ๋ ๋ง์ client ์ฒ๋ฆฌ ๊ฐ๋ฅ
- Do not use Node for CPU-intensive apps (ex) video encoding, image manipulation service) -> ์๋ํ๋ฉด GPU๊ฐ ํ๋ ๊ณ์ฐ ๋ง๊ณ ํ์ผ ์์คํ ์ด๋ ๋คํธ์ํฌ๋ ๋ณ๋ก ์๊ฑด๋ค์.
- Only use in data-intensive and real-time apps
Node.js ์ค์น
cmd ์ฐฝ์์ ์ค์น ํ์ธ : node --version
์์ผ๋ฉด ์ค์น -> https://nodejs.org/en/
Build our first application
1) console (cmd)
> mkdir first-app ----> 'first-app'ํด๋ ์์ฑ
> cd first-app/
> code . ----> visual studio code๋ก ๊ฐ
2) (visual studio code์์) ์ ํ์ผ app.js ๋ง๋ฆ
function sayHello(name){
console.log(‘Hello ’+name);
}
sayHello(‘Mosh’);
3) ํฐ๋ฏธ๋(cmd)์์ ์คํ
> node app.js
app.jsํ์ผ ์คํ -> ๊ฒฐ๊ณผ: Hello Mosh
- Console.log(window) ์คํ
- ๊ฒฐ๊ณผ: window is not defined -> window๋ document object๋ฅผ ๊ฐ๊ณ ์์ง ์์
Node core
Node Module System
- ๋ ธ๋ ์ฝ์ด์ ๊ตฌ์ถ๋์ด ์๋ ๋ชจ๋ : os, fs, events, http
- ๋๋ง์ ๋ชจ๋์ ๋ง๋๋ ๋ฐฉ๋ฒ๋ ๋ฐฐ์ธ ๊ฒ์ด๋ค.
Global Object (์ ์ญ ๊ฐ์ฒด)
Console.log(); // console object๋ ์ด๋ค ํ์ผ์์๋ ์ ๊ทผํ ์ ์๋ ์ ์ญ๋ณ์
ex) setTimeout(), clearTimeout(), setInterval(), clearInterval() ๋ฑ
-> Window object์ ์ํด ์์
๋ ธ๋ -> global์ด๋ผํ๋ ๊ฐ์ฒด์ ์ํด ์์
- Var message = ''; ----> ์ ์ญ๋ณ์๊ฐ ์๋๋ฏ๋ก ํด๋นํ์ผ ๋ฐ์์๋ ์ ๊ทผ ๋ชปํจ.
Modules
- ์์ ์ ์ด๊ณ ์ ๋ขฐ๊ฐ ์๋ ํ๋ก๊ทธ๋จ์ ๋ง๋๋ ค๋ฉด ์ ์ญ๋ณ์๋ ์ ์ญํจ์์ ์ง์ ์ ํผํด์ผ ํ๋ค.
- ๋์ Module ์ฌ์ฉ
Creating a Module
// logger.js
var url = 'http://mylogger.io/log'; // imagine send a html request to this url(end point)
function log(message) {
// Send an HTTP request
console.log(message);
}
module.exports.log = log; //global ๋ณ์๋ก ์ง์ ํ๊ณ ์ถ์ด์
- main module(app.js)์์ logger.js์ ํจ์ log๋ฅผ ์ ๊ทผํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
- module.exports.log = log; ๋ก log๋ฅผ global ๋ณ์ log๋ก ์ง์
Loading a Module
- require(): (๋ธ๋ผ์ฐ์ ์๋ ์๋ ํจ์) loadํ๊ณ ์ถ์ ๋ชจ๋์ ์ธ์๋ก ๋๊ธด๋ค.
- app.js
// app.js
var logger = require('./logger');
console.log(logger);
๋ชจ๋์ด ๋์ํ๋ ๋ฐฉ๋ฒ
[์ฝ๋]
- app.js
// app.js
const logger = require('./logger');
logger.log('message');
logger์ ์ฌ์ ์๋ฅผ ๋ง๊ธฐ์ํด ์์๋ก ์ง์ ํ๋ ๊ฒ์ด ์์ ํ๋ค.
- logger.js
// logger.js
var url = 'http://mylogger.io/log';
function log(message) {
// Send an HTTP request
console.log(message);
}
module.exports.log = log;
Module Wrapper Function
- Node๋ ์ฝ๋๋ฅผ ์ฆ๊ฐ์ ์ผ๋ก ๋ฐ๋ก ์คํํ์ง ์๋๋ค.
(function(exports, require, module, __filename, __dirname){
// code…
})
์ด๋ฐ์์ผ๋ก ์ฝ๋๋ฅผ ๊ฐ์ผ ํ ์คํ
// Logger.js์ ์ถ๊ฐ
console.log(__filename);
console.log(__dirname);
์ผ๋ก ํ์ธ
<ํฌ๊ฒ ์์์ผ ํ module>
File System, HTTP(์น์๋ฒ), OS, Path(์ ํธ๋ฆฌํฐ ๊ธฐ๋ฅ), Process, Query Strings, Stream ๋ฑ
(์ฐธ๊ณ : https://nodejs.org/dist/latest-v12.x/docs/api/)
Path module
- path ๋ชจ๋
const path = require('path');
var pathObj = path.parse(__filename);
console.log(pathObj);
OS module
const os = require('os');
var totalMemory = os.totalmem();
var freeMemory = os.freemem();
console.log(`Total Memory: ${totalMemory}`);
console.log(`Free Memory: ${freeMemory}`);
์๋ฐ์คํฌ๋ฆฝํธ์์๋ ์์ ์๋ ์ ๋ณด๋ค์ ์ ์ ์์
File System module
const fs = require('fs');
1) synchronous์ธ readdirSync() ํจ์
const files = fs.readdirSync('./');
console.log(files);
ํ์ง๋ง, ๋ ธ๋์์๋ "asynchronous ํจ์ ์ฌ์ฉ"์ ์ ๊ทน ๊ถ์ฅํ๋ค!
2) asynchronous์ธ readdir() ํจ์
// asynchronous function
const files = fs.readdir('./', function(err, files){
if(err) console.log('Error', err);
else console.log('Result', files);
});
- ๋๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ call back ํจ์
- node๋ asynchronous operation์ด ๋๋๋ฉด ๋๋ฒ์งธ ์ธ์์ ํจ์๋ฅผ ๋ถ๋ฅธ๋ค.
Events module
- Event: A signal that something has happened
const EventEmitter = require('events');
// class์
const emitter = new EventEmitter();
// Register a listener
emitter.on('messageLogged', function(){
console.log('Listener called');
})
// Raise an event
emitter.emit('messageLogged', { id: 1, url: 'http://'});
// emit: Making a noise, produce - signalling
- emit์ด ๋ชจ๋ ์ด๋ฒคํธ๋ฅผ sychoronously ํ๊ฒ ๋ถ๋ฌ์ค๊ธฐ ๋๋ฌธ์ emit์ด ์์ผ๋ฉด ์ถ๋ ฅ ์๋จ
- ์ด ๊ธฐ์ ๋ก ๋ฐฉ๊ธ ๋ฐ์ํ ์ด๋ฒคํธ์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ค ์ ์๋ค.
- ์ด๋ ๊ฒ ์ฝ๋ฐฑ์ ์ฐ๊ธฐ๋ ํจ (์ด๊ฑธ ์ ํธ)
// Register a listener
emitter.on('messageLogged', (arg) => { // e, eventArg์ด๋ผํด๋๋จ
console.log('Listener called', arg);
})
Extending EventEmitter
- ์๋ก ๋ค๋ฅธ ํ์ผ์์ emitter์ฐ๋ ค๊ณ ๊ฐ ํ์ผ๋ง๋ค
- const emitter = new EventEmitter();
- ์ ์ ์ธํ๋ฉด ์ด๋ฆ์ ๊ฐ์ง๋ง ์๋ก ๋ค๋ฅธ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋๋ค.
=> ๋์ class๋ฅผ ๋ง๋ ๋ค
[app.js]
// app.js
const EventEmitter = require('events');
const Logger = require('./logger');
const logger = new Logger();
// Register a listener
logger.on('messageLogged', (arg) => {
console.log('Listener called', arg);
})
logger.log('message');
[logger.js]
// logger.js
const EventEmitter = require('events');
var url = 'http://mylogger.io/log';
class Logger extends EventEmitter{
//base class
log(message) {
// Send an HTTP request
console.log(message);
// Raise an event
this.emit('messageLogged', { id: 1, url: 'http://'});
}
}
module.exports = Logger; // export Logger class
EventEmitter๋ฅผ ํ์ฅํ์ฌ EventEmitter๊ฐ Loggerํด๋์ค์ ์ผ๋ถ๊ฐ ๋จ
-> this๋ฅผ ์ฌ์ฉํ์ฌ EventEmitter์ method ์ฌ์ฉ๊ฐ๋ฅ
HTTP module
: ์ฑ์์ ๋คํธ์ํฌ๋ฅผ ๋ง๋๋๋ฐ ์ฌ์ฉ๋๋ ๋ชจ๋
// app.js
const http = require('http');
const server = http.createServer(); //create web server
// this server is event emitter
/* http.Serverํด๋์ค๋ net.Server๋ฅผ ์์๋ฐ์ ํด๋์ค์ธ๋ฐ
net.Server ๊ฐ event emitter์ด๋ค (๊ณต์๋ฌธ์์์ ํ์ธ)
*/
server.on('connection', (socket) => {
console.log('New connection...');
})
server.listen(3000);
console.log('Listening on port 3000...');
- ์ฝ๋
[app.js]
const http = require('http');
const server = http.createServer(function(req, res){
if(req.url === '/'){
res.write('Hello World');
res.end();
}
if(req.url === '/api/courses'){
res.write(JSON.stringify([1,2,3])); // convert this array->JSON syntex
res.end();
}
// ํ์ง๋ง ์ด๋ ๊ฒ ์ฌ๋ฌ ๊ฐ์ ๋ผ์ฐํฐ๋ฅผ ๊ฐ์ง ๊ฒ์ ์ฝ๋๋ฅผ ๋ณต์กํ๊ฒ ๋ง๋ฆ
// express ์ฌ์ฉ -> clean structure
}); //create web server
server.listen(3000);
console.log('Listening on port 3000...');
- ์คํ๊ฒฐ๊ณผ
[์ ๋ฆฌ from]
Node.js Tutorial for Beginners (์ฑ๋: Programming with Mosh)
'๐ Archive > Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Express.js ํํ ๋ฆฌ์ผ (GET, POST, PUT, DELETE) (0) | 2020.03.04 |
---|