강상위

Freature Skeleton - login, join, DB save, DB search

This diff is collapsed. Click to expand it.
......@@ -13,7 +13,11 @@
"author": "",
"license": "ISC",
"dependencies": {
"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",
"request": "^2.88.0",
......
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
//var session = require('express-session');
var mongoose = require('mongoose');
//DB연결
mongoose.connect('mongodb://username:pwd@1.201.139.92/dbname');
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');
// 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 userSchema = mongoose.Schema
(
{
id: String,
pwd: String,
name: String
}
);
module.exports = mongoose.model('user',userSchema);
......@@ -39,4 +39,4 @@ TestModel.find(function(err, test){
});
//db.close()
\ No newline at end of file
db.close()
\ No newline at end of file
......
module.exports = function(app, Users)
{
app.get('/', function(req, res)
{
res.render("index");
console.log("The index page!")
});
// 로그인 수행 - POST
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으로 이동
}
});
});
// 메인화면 - 로그인 후 기본 검색화면
app.get('/main', function(req,res)
{
res.render("main");
console.log("The test page!")
});
// Join
app.route('/join')
.get(function(req, res) // 처음 Join화면 랜더 - GET
{
res.render("join")
})
.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('/');
}
});
});
}
\ 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">
<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>
</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
<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">
<button>로그아웃</button>
<button>검색</button>
<button>마이페이지</button>
<button>나만의시간표</button>
</div>
\ No newline at end of file