정적파일 제공

app.use(express.static(__dirname + ‘/public’)); 이걸보고 __dirname이 뭔지 궁금해서 구글링해봤다. 찾아보니.. 만약 server.js파일이 /workspace/practice 에 속해있다면 __dirname은 /workspace/practice가 되는 것이다.

app.use(express.static('public')); //이렇게하면 public이라는 디렉토리의 정적파일들을 사용하겠다는 의미이다.
app.use(express.static(__dirname+' /public'));
express.static 함수에 제공되는 경로는 node 프로세스가 실행되는 디렉토리에 대해 상대적이다. Express 앱을 다른 디렉토리에서 실행하는 경우에는 다음과 같이 제공하기 원하는 디렉토리의 절대 경로를 사용하는 것이  안전하다고 한다.

ejs란 뭘까?

node.js의 뷰 템플릿 엔진으로 , express에서 사용할 수 있다. ``` app.get(‘/create’,function(req,res){ fs.readFile(‘create.ejs’,’utf8’,function(error,data){ response.send(data); }); }); 이런식으로 ejs 뷰를 route해줄 수 있다.

# emit()과 on()

구글링해서 찾아보니 잘된 설명이 있다. 그대로 가져와 봤다. In node.js an event can be described simply as a string with a corresponding callback. An event can be “emitted” (or in other words, the corresponding callback be called) multiple times or you can choose to only listen for the first time it is emitted.
The on or addListener method (basically the subscription method) allows you to choose the event to watch for and the callback to be called. The emit method (the publish method), on the other hand, allows you to “emit” an event, which causes all callbacks registered to the event to ‘fire’, (get called).

한마디로 on은 이벤트가 발생하는지 봐서 callback함수를 실행하는 것이고, emit은 event를 객체로하여금 발생시키는 것이다.

Written on