HJW

Made fortune cookie module. and applied it!

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