[Node.js] commonJS / AMD ๐Ÿ’ฏ๏ธ

jungeundelilahLEEยท2020๋…„ 12์›” 28์ผ
0

Node.js

๋ชฉ๋ก ๋ณด๊ธฐ
13/27

goal

node.js ๐Ÿ’ฏ๏ธ

์›น์„œ๋ฒ„

  • http๋ผ๋Š” ๋ชจ๋“ˆ์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„๋ฅผ ์ƒ์„ฑ
const server = require('http');

server.createServer(function(req, res){
  res.writeHead(200, { 'Content-Type' : 'text/plain' });
  res.end("Hello node.js! \n");
}).listen(3000, 'localhost');

console.log('Server is running at http://localhost:3000/');

ํŒŒ์ผ ์ฝ๊ธฐ

  • fs๋ผ๋Š” ํŒŒ์ผ ๋ชจ๋“ˆ์„ ๊ฐ€์ง€๊ณ  ์™€์„œ readFile ์ด๋ผ๋Š” ๋น„๋™๊ธฐ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ ์ฝ๊ณ  ํ•ด๋‹น ๋‚ด์šฉ์„ ์ถœ๋ ฅ
const fs = require("fs"); 

fs.readFile('./hello.txt', encoding = 'utf-8', function(err, data) {
  if (err) {
    throw err;
  }
  console.log(data + " Node.js!");
});
///////////////////////////////////////////
hello.txt
์•ˆ๋…• ๋‚˜๋Š” ์ •์€์ด์•ผ!

๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ์ด์šฉ

  • events๋ผ๋Š” ๋ชจ๋“ˆ์€ ๋…ธ๋“œ์—์„œ ๋น„๋™๊ธฐ ์ด๋ฒคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋„๋ก ํ•ด์ฃผ๋Š” ๋ชจ๋“ˆ
var EventEmitter = require("events").EventEmitter;
var evt = new EventEmitter();

evt.on("helloNode", function(str) {
  console.log("Hello! " + str );
});

setTimeout(function() {
  evt.emit("helloNode", "Node.js");
}, 3000);

commonJS ๐Ÿ’ฏ๏ธ

node.js๋Š” common.js๊ทœ๊ฒฉ์„ ๋”ฐ๋ฆ„

  • <script src="require.js"></script> ๋กœ ๋ถˆ๋Ÿฌ์˜จ ํ›„, module.exports๋กœ ๋ชจ๋“ˆํ™”์‹œํ‚ค๊ณ  ์‹ถ์€ ๋ณ€์ˆ˜๋ฅผ ๋ฌถ์–ด์ค€๋‹ค.
  • exports๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
// main.js
var exports = module.exports = {};
// ์‚ฌ์‹ค์ƒ ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
const http = require('http');
const delilah = require('delilah');
module.exports = {
  a: http,
  b: delilah,
};
const main = require('main');
console.log(main.a, main.b);
  • ๋ชจ๋“ˆ์˜ ํ•ต์‹ฌ์€ ๋งŒ๋“ค์–ด์„œ ๋ฐ”๊นฅ์œผ๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๊ฒƒ
  • ๋ชจ๋“  ๋ชจ๋“ˆ์€ ์ž์‹ ๋งŒ์˜ ๋…๋ฆฝ์ ์ธ ์‹คํ–‰ ์˜์—ญ์„ ๊ฐ–์Œ : exportsํ•  ๊ฒƒ๊ณผ ํ•˜์ง€ ์•Š์„ ๊ฒƒ์„ ๊ตฌ๋ถ„
  • ๋ชจ๋“ˆ ์ •์˜๋Š” ์ „์—ญ๊ฐ์ฒด์ธ exports๊ฐ์ฒด๋ฅผ ์ด์šฉ
  • ๋ชจ๋“ˆ ์‚ฌ์šฉ์€ requireํ•จ์ˆ˜๋ฅผ ์ด์šฉ

module.exports VS exports

  • exports๋Š” module.exports ์‚ฌ์šฉ์„ ๋„์™€์ฃผ๋Š” helper
  • exports๋Š” module.exports๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์—ญํ•  only
  • module.exports์— ์ด๋ฏธ ๊ฐ’์ด ํ• ๋‹น๋˜๋ฉด, exports๋Š” ๋ฌด์‹œ๋จ
  • ๋”ฐ๋ผ์„œ, ๋‘˜์„ ์„ž์–ด์„œ ์“ฐ์ง€ ๋ง์•„์•ผ ํ•จ


๋ฌธ์ œํ’€์ด๋กœ ์•Œ์•„๋ณด๋Š” Common JS ๐Ÿ’ฏ๏ธ

[ ๋ฌธ์ œ 2 ]

// Assume you have the following in subject.js:
let mod1 = require('./lib/my-module.js');
let mod2 = require('./lib/my-module.js');
mod1.increment();
let result = mod2.increment();

// ...and the following in lib/my-module.js:
let counter = 0;
exports.increment = function () {
  counter += 1;
  return counter;
};

// After subject.js runs, what will be the value of result?
// ๋‚˜์˜ ์˜ค๋‹ต : 1
// ์ •๋‹ต : 2  

[ ๋ฌธ์ œ 3 ]

// Assume you have the following in subject.js:
let mod1 = require('./lib/my-module.js');
let mod2 = require('./lib/my-module.js');

// ...and the following in lib/my-module.js:
console.log("Loading module!")

// After subject.js runs, how many logs in the console will you see?
// ๋‚˜์˜ ์˜ค๋‹ต : 2
// ์ •๋‹ต : 1
// ํ•ด์„ค : require์— ๊ฐ™์€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋‘๋ฒˆ ๊ฐ€์ ธ์˜ค๋”๋ผ๋„ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋œ๋‹ค. 
// ์บ์‹ฑ๋œ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉ
// ์บ์‹œ๊ฐ€ ์ด๋ฏธ์žˆ๋‹ค๋ฉด 'ํŒŒ์ผ์„ ์‹คํ–‰ํ–ˆ๋‹ค'๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด ์—ฌ๋Ÿฌ๋ฒˆ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

[ ๋ฌธ์ œ 4 ]

// Assume superLongComputation() is synchronous and 5 seconds to run. After all setTimeout callbacks have run, what order of letters will be shown in the console?

console.log("A");

setTimeout(function() {
  console.log("B");
}, 1000);

superLongComputation(); // console.log("5์ดˆ ๋!")

setTimeout(function() {
  console.log("C");
}, 500);

console.log("D");

// ๋‚˜์˜ ์˜ค๋‹ต : A D C B
// ์ •๋‹ต : A D B C
// A => 5์ดˆ ๋ => D => "B => C" ์˜ ์ด์œ ๋Š” ์•ž์˜ ํ•จ์ˆ˜๊ฐ€ 5์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๊ธฐ๊ฐ„์— 1000 ์งœ๋ฆฌ๊ฐ€ ์ฝœ์Šคํƒ์œผ๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Œ

[ ๋ฌธ์ œ 5 ]

// After all setTimeout callbacks have run, what order of letters will be shown in the console?

console.log("A");

setTimeout(function() {
  console.log("B");
}, 500);

setTimeout(console.log("C"), 1000);

// ๋‚˜์˜ ์˜ค๋‹ต : A B C
// ์ •๋‹ต : A C B
// ํ•ด์„ค : settimeout์˜ ์ธ์ž๋กœ๋Š” ์ต๋ช…ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ๋˜๋Š”๋ฐ, ์ฝ˜์†”์ด ๋“ค์–ด๊ฐ”์œผ๋ฏ€๋กœ, settimeout์ด ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

[ ๋ฌธ์ œ 6 ]

// After all setTimeout callbacks have run, what order of letters will be shown in the console?

console.log("A");

setTimeout(go(), 100);
// settimeout (console.log("x"),100) ์ด๋˜์–ด settimeout์ด ์ž‘๋™ํ•˜์ง€ ์•Š์Œ

setTimeout(function() {
  console.log("B");
}, 50);

function go () {
  console.log("X")
}
// ์ •๋‹ต : A X B

[ ๋ฌธ์ œ 7 ]

// After all setTimeout callbacks have run, what order of letters will be shown in the console?

console.log("A");

setTimeout(go, 100);
// setTimeout(function() {console.log("X")}, 100) ์™€ ๊ฐ™์Œ

setTimeout(function() {
  console.log("B");
}, 50);

function go () {
  console.log("X")
}
// ์ •๋‹ต : A B X 
// 

AMD (Asynchronous Module Definition, ๋น„๋™๊ธฐ์  ๋ชจ๋“ˆ ์„ ์–ธ) ๐Ÿ’ฏ๏ธ

  • <script src="require.js"></script> ๋กœ ๋ถˆ๋Ÿฌ์˜จ ํ›„, ๋ณธ์ธ์ด ์›ํ•˜๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค์„ define์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž์ธ ๋ฐฐ์—ด ์•ˆ์— ๋‚˜์—ดํ•œ ํ›„, ๋‘๋ฒˆ์งธ ์ธ์ž์ธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.
define(['jquery', 'delilah'], function(apple, banana) {
  return {
    a: apple,
    b: banana,
  }
});
profile
delilah's journey

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