강상위

Merge branch 'ksw'

node_modules
mongodb
test.js
test_api.js
......
# 나만의 편성표 - My Personal Broadcating Schedule
## Description
- 오픈소스SW개발 프로젝트
- 관심있는 인물이 출연하는 방송 프로그램을 검색하여 나만의 편성표를 만든다.
## Environment
- Backend - Node.js / Express
- Frontend - HTML5/CSS/Javascript
- DB - MongoDB
## Prerequisite
- Terminal Environment
- 1.Clone
> git clone \<THIS-PROJECT\>
- 2.Install modules
> cd \<THIS-PROJECT\>
> npm install
- 3.Run
\ No newline at end of file
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var session = require('express-session');
var mongoose = require('mongoose');
var passport = require('passport');
//DB연결
mongoose.connect('mongodb://username:pwd@host/dbname');
mongoose.Promise = global.Promise;
var db = mongoose.connection;
//연결실패
db.on('error', function()
{
console.log('Connection Failed!');
});
//연결 성공
db.once('open', function()
{
console.log('Connected!');
});
// DB모델정의
var Users = require('./models/users');
// session
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
}));
// passport setting
require('./passport')(passport);
app.use(passport.initialize());
app.use(passport.session()); //로그인 세션 유지
// 주의! passport.session을 사용하기 전에 app.use(session(~))설정을 해줘야 한다.
// 그렇지 않으면 passport가 session을 사용하지 못한다.
// app.use는 동기식으로 작동하기 때문에 순서에 유의해야한다.
// ejs사용
// json사용설정
app.set('view engine','ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// router import
var router = require('./routing')(app, Users);
var server = app.listen(23023, function()
{
var host = server.address().address;
var port = server.address().port;
console.log("http://%s:%s",host, port);
});
\ No newline at end of file
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var userSchema = mongoose.Schema
(
{
id: String,
pwd: String,
name: String
}
);
// 패스워드 암호화
userSchema.methods.generateHash = function(password)
{
// password hash를 만든다
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// 패스워드 검증
userSchema.methods.validPassword = function(password)
{
// 기존의 해쉬값과 들어온 패스워드를 해쉬값으로 만든 값을 비교한다.
// 주의! 기존의 값이 해쉬가 아니라면 비교불가. 따라서 에러.
// 또한 나는 bcrypt를 bcryt로 잘못 썼는데 잘 안보인다... 조심해라...
//var good = bcrypt.hashSync(this.pwd, bcrypt.genSaltSync(8), null);
return bcrypt.compareSync(password, this.pwd);
};
module.exports = mongoose.model('user',userSchema);
var mongoose = require('mongoose');
mongoose.connect('mongodb://username:pwd@host/dbname');
var db = mongoose.connection;
//연결실패
db.on('error', function(){
console.log('Connection Failed!');
});
//연결 성공
db.once('open', function() {
console.log('Connected!');
});
var testSchema = mongoose.Schema
({
name: String
});
var TestModel = mongoose.model("TestModel", testSchema);
/*
var test = new TestModel({ name: "test" });
test.save(function(err, test)
{
if(err){console.log(err);}
else{console.log("Success!");}
console.log("ok4");
});
*/
TestModel.find(function(err, test){
if(err){console.log(err);}
else{
console.log(test);
}
});
db.close()
\ No newline at end of file
This diff is collapsed. Click to expand it.
{
"name": "my-broadcasting-ksw",
"version": "1.0.0",
"description": "- 오픈소스SW개발 프로젝트 - 관심있는 인물이 출연하는 방송 프로그램을 검색하여 나만의 편성표를 만든다.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "http://khuhub.khu.ac.kr/2013104043/my-broadcasting.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.18.3",
"cheerio": "^1.0.0-rc.2",
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-session": "^1.15.6",
"iconv": "^2.3.1",
"mongoose": "^5.3.14",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"request": "^2.88.0",
"selenium-webdriver": "^4.0.0-alpha.1"
}
}
var LocalStrategy = require('passport-local').Strategy;
var Users = require('./models/users');
module.exports = function(passport)
{
passport.serializeUser(function(user, done)
{
done(null, user.id);
});
passport.deserializeUser(function(id, done)
{
done(null, id);
/*
Users.findById(id, function(err, user)
{
done(err, user);
});
*/
});
// 회원가입 처리
passport.use('join', new LocalStrategy
(
{
usernameField: 'id',
passwordField: 'pwd',
passReqToCallback: true
},
function(req, id, pwd, done)
{
Users.findOne({'id':id}, function(err, user)
{
if(err) return done(err);
// 유저가 있을 경우 처리
if(user)
{
console.log("Duplicated user");
return done(null, false);
}
// 새로운 유저 DB추가처리
else
{
var newUser = new Users();
newUser.id = id;
newUser.pwd = newUser.generateHash(pwd);
// 로그인 이외 필요한 값들 추가
newUser.name = req.body.name;
newUser.save(function(err)
{
if(err) throw err;
return done(null, newUser);
});
}
});
}));
// 로그인 처리
passport.use('login', new LocalStrategy
(
{
usernameField: 'id',
passwordField: 'pwd',
passReqToCallback: true
},
function(req, id, pwd, done)
{
Users.findOne({'id': id}, function(err, user)
{
if(err) return done(err);
// 유저가 없을 시
if(!user)
{
console.log('no user');
return done(null, false);
//return done(null, false, req.flash('loginMessage', '없는 유저입니다..'));
}
// 틀린 비밀번호
if(!user.validPassword(pwd))
{
console.log('bad password');
return done(null, false);
//return done(null, false, req.flash('loginMessage', '비밀번호가 다릅니다.'));
}
console.log('login sucess');
return done(null, user);
});
}
));
}
\ No newline at end of file
module.exports = function(app, Users)
{
var passport = require('passport');
app.get('/', function(req, res)
{
// 로그인 중이면 메인페이지로
if(req.isAuthenticated())
res.redirect("/main");
// 로그인 중이 아니라면 인덱스페이지
else
{
res.render("index");
console.log("The index page!");
}
});
// 로그인 수행 - POST
app.route('/login')
.post(passport.authenticate('login',
{
successRedirect: '/main',
failureRedirect: '/'
//failureFlash : true
}))
// unexpected access
.get(function(req, res)
{
res.redirect("/");
});
// 로그아웃 수행
app.get('/logout', function(req, res)
{
req.logout();
res.redirect('/');
})
// Join
app.route('/join')
// 처음 Join화면 랜더 - GET
.get(function(req, res)
{
res.render("join")
})
// 실제 Join 수행 - POST
.post(passport.authenticate('join',
{
successRedirect: '/main',
failureRedirect: '/',
//failureFlash : true
}));
// 메인화면 - 로그인 후 기본 검색화면으로
app.get('/main', function(req, res)
{
// 로그인 중이라면
if(req.isAuthenticated())
{
console.log("Logged in page");
res.render("main");
}
// 로그인 중이 아니라면
else res.redirect("/");
});
// 마이페이지 - 로그인 필수
app.get('/mypage',function(req, res)
{
// 로그인 중이라면
if(req.isAuthenticated())
{
// find를 쓰면, 다행으로 반환되기 때문에 결과의 첫번째 요소를 지정하고 해야함
// 그래서 하나만을 대상으로 할 때는 보통 findOne을 사용
// mongoose로 디비 find는 콜백으로 정의해야함.
Users.findOne({id: req.user}, function(err, user_info)
{
console.log("mypage");
res.render("mypage",
{
id: user_info.id,
name: user_info.name
});
});
}
// 로그인 중이 아니라면
else res.redirect("/");
});
// 동명이인 검색 페이지
app.get("/samename" ,function(req, res)
{
var samename_list = [["강호동", "https://search.pstatic.net/common?type=a&size=60x76&quality=95&src=http://sstatic.naver.net/people/portrait/201304/20130403113314207.jpg"], ["강호동", "https://search.pstatic.net/common?type=a&size=60x76&quality=95&src=http://sstatic.naver.net/people/72/201601061648058211.jpg"]];
console.log("samename page");
res.render("samename",{samename : samename_list});
});
// 인물-방송정보 페이지
app.get("/programs", function(req, res)
{
var program_list =
[
["https://search.pstatic.net/common?type=o&size=120x172&quality=90&direct=true&src=http%3A%2F%2Fsstatic.naver.net%2Fkeypage%2Fimage%2Fdss%2F57%2F03%2F30%2F31%2F57_9033031_poster_image_1543806768348.jpg",
"2018",
"아모르파티",
true,
"SUN",
"2240"],
["https://search.pstatic.net/common?type=o&size=120x172&quality=90&direct=true&src=http%3A%2F%2Fsstatic.naver.net%2Fkeypage%2Fimage%2Fdss%2F57%2F03%2F30%2F31%2F57_9033031_poster_image_1543806768348.jpg",
"2018",
"아모르파티",
true,
"SUN",
"2240"]
];
res.render("programs", {programs: program_list});
});
// 나만의 시간표
app.get("/timetable", function(req, res)
{
var program_list =
[
{
content: '런닝맨',
endDate: new Date(2018, 11, 9, 5, 45),
startDate: new Date(2018, 11, 9, 1, 30),
disabled: true
}
];
res.render("timetable", {pl: JSON.stringify(program_list)});
});
}
/*
(구)직접 DB에 저장하기
.post(function(req, res) // 실제 Join 수행 - POST
{
// user정보 입력
var user = new Users();
user.id = req.body.id;
user.pwd = req.body.pwd;
user.name = req.body.name;
// DB저장
user.save(function(err)
{
if(err)
{
console.log(err);
res.send("Error!")
}
else
{
console.log("Join Success");
res.redirect('/');
}
});
});
*/
/*
(구)직접 로그인 하기
app.post('/login', function(req, res)
{
Users.find({id: req.body.id, pwd: req.body.pwd},{_id: 1}, function(err, user)
{
if(err)
{
console.log("Error!");
res.send("Error!")
}
// 매칭정보 없음 - 로그인 실패
if(user.length==0)
{
console.log("Login failed!")
res.send("Login_failed");
}
// 매칭정보 있음 - 로그인 성공
else
{
console.log("Login Success!")
res.redirect("/main");
// main으로 이동
}
});
});
*/
\ No newline at end of file
<div id="wrapper">
<div id="myScheduler"></div>
</div>
<script>
setTimeout(() =>
{
YUI().use('aui-scheduler',
function(Y)
{
// code goes here
//console.log(<%- pl %>);
var events =
[
{
content: '<%- pl[0].content%>',
endDate: new Date(2018, 11, 9, 5, 30),
startDate: new Date(2018, 11, 9, 1, 30),
disabled: true
}
];
var weekView = new Y.SchedulerWeekView();
myScheduler = new Y.Scheduler(
{
boundingBox: '#myScheduler',
date: new Date(Date.now().getYear, Date.now().getMonth, Date.now().getDay),
items: events,
render: true,
views: [weekView]
}
);
});
}, 1000);
</script>
\ No newline at end of file
<div class="contents_index">
<form method="POST" action="/login">
<label>id:</label><input type="text" name="id"><br/>
<label>pwd:</label><input type="password" name="pwd"><br/>
<button type="submit">로그인</button>
</form>
</div>
\ No newline at end of file
<div class="contents_index">
<form method="POST" action="/join">
<label>id:</label><input type="text" name="id"><br/>
<label>pwd:</label><input type="password" name="pwd"><br/>
<label>name:</label><input type="text" name="name"><br/>
<button type="submit">가입</button>
</form>
<a href='/'><button>취소</button></a>
</div>
\ No newline at end of file
<div class="contents_main">
<h1>This is main</h1>
<form method="GET" action="/samename">
<input type="text" name="samename">
<button type="submit">검색</button>
</form>
</div>
\ No newline at end of file
<div class="contents_main">
<h1>mypage</h1>
<div><label>id:</label><%=id %></div>
<div><label>name:</label><%=name %></div>
</div>
\ No newline at end of file
<div class="contents_main">
<h1>This is Programs</h1>
<% programs.forEach(function(val){ %>
<li><img src="<%= val[0]%>"></li>
<li><%= val[1]%></li>
<li><%= val[2]%></li>
<% if(val[3] == true) { %>
<li><%= val[4] %></li>
<li><%= val[5] %></li>
<button>추가</button>
<% } %>
<% }) %>
</div>
\ No newline at end of file
<div class="contents_main">
<h1>This is Samename</h1>
<% samename.forEach(function(val){ %>
<li><%= val[0]%></li>
<li><img src="<%= val[1]%>"></li>
<% }) %>
</div>
\ No newline at end of file
<div class="contents_main">
<h1>This is TimeTable</h1>
<table border="1">
<tr>
<td>*</td>
<td>월</td>
<td>화</td>
<td>수</td>
<td>목</td>
<td>금</td>
<td>토</td>
<td>일</td>
</tr>
<% for(var i=0; i<=24; i++) { %>
<tr>
<td><%=i %></td>
<% for(var j=1; j<=7; j++) { %>
<td><%=j %></td>
<% } %>
</tr>
<% } %>
</table>
</div>
\ No newline at end of file
<html>
<head>
<title>Index</title>
</head>
<body>
<% include ./navigation_index.ejs %>
<% include ./contents_index.ejs %>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>Join</title>
</head>
<body>
<% include ./navigation_index.ejs %>
<% include ./contents_join.ejs %>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>Main</title>
</head>
<body>
<% include ./navigation_main.ejs %>
<% include ./contents_main.ejs %>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>MyPage</title>
</head>
<body>
<% include ./navigation_main.ejs %>
<% include ./contents_mypage.ejs %>
</body>
</html>
\ No newline at end of file
<div class="navigation_index">
<a href='/join'><button>회원가입</button></a>
<a href='/'><button>Index</button></a>
</div>
\ No newline at end of file
<div class="navigation_main">
<a href="/logout"><button>로그아웃</button></a>
<a href="/main"><button>검색</button></a>
<a href="/mypage"><button>마이페이지</button></a>
<a href="/timetable"><button>나만의시간표</button></a>
</div>
\ No newline at end of file
<html>
<head>
<title>Programs</title>
</head>
<body>
<% include ./navigation_main.ejs %>
<% include ./contents_programs.ejs %>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>Samename</title>
</head>
<body>
<% include ./navigation_main.ejs %>
<% include ./contents_samename.ejs %>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>My Timetable</title>
<script src="https://cdn.alloyui.com/3.0.1/aui/aui-min.js"></script>
<link href="https://cdn.alloyui.com/3.0.1/aui-css/css/bootstrap.min.css" rel="stylesheet"></link>
</head>
<body>
<% include ./navigation_main.ejs %>
<% include ./alloyscheulertest.ejs %>
</body>
</html>
\ No newline at end of file