HJW

Made fortune cookie module. and applied it!

/**
* 모듈 만들기
*/
var fortunes = [
"Conquer your fears or they will conquer you.",
"Rivers need springs.",
"Do not fear what you don't know.",
"You will have a pleasant surprise.",
"Whenever possible, keep it simple.",
];
//전역변수 추가. 모듈 밖에서도 사용할 수 있게.
exports.getFortune = function() {
var idx = Math.floor(Math.random() * fortunes.length);
return fortunes[idx];
}
var express = require('express');
//require : 모듈 가져오는 함수
//include 와 비슷한 느낌이군.
//기본적으로 node_modules 디렉토리에서 찾는다
// ./를 붙이면 거기서 찾지 않는다.
var fortune = require('./lib/fortune.js');
var fortunes = [
"Conquer your fears or they will conquer you.",
"Rivers need springs.",
"Do not fear what you don't know.",
"You will have a pleasant surprise.",
"Whenever possible, keep it simple.",
];
var app = express();
app.use(express.static(__dirname + '/public'));
......@@ -21,8 +19,8 @@ app.get('/', function (req, res) {
res.render('home');
});
app.get('/about', function (req, res) {
var randomFortune = fortunes[Math.floor(Math.random() * fortunes.length)];
res.render('about', { fortune : randomFortune});
// var randomFortune = fortunes[Math.floor(Math.random() * fortunes.length)];
res.render('about', { fortune : fortune.getFortune()});
});
//404 Page
......