강상위

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

This diff is collapsed. Click to expand it.
...@@ -13,7 +13,11 @@ ...@@ -13,7 +13,11 @@
13 "author": "", 13 "author": "",
14 "license": "ISC", 14 "license": "ISC",
15 "dependencies": { 15 "dependencies": {
16 + "body-parser": "^1.18.3",
16 "cheerio": "^1.0.0-rc.2", 17 "cheerio": "^1.0.0-rc.2",
18 + "ejs": "^2.6.1",
19 + "express": "^4.16.4",
20 + "express-session": "^1.15.6",
17 "iconv": "^2.3.1", 21 "iconv": "^2.3.1",
18 "mongoose": "^5.3.14", 22 "mongoose": "^5.3.14",
19 "request": "^2.88.0", 23 "request": "^2.88.0",
......
1 +var express = require('express');
2 +var app = express();
3 +var bodyParser = require('body-parser');
4 +//var session = require('express-session');
5 +var mongoose = require('mongoose');
6 +
7 +//DB연결
8 +mongoose.connect('mongodb://username:pwd@1.201.139.92/dbname');
9 +var db = mongoose.connection;
10 +
11 +//연결실패
12 +db.on('error', function()
13 +{
14 + console.log('Connection Failed!');
15 +});
16 +
17 +//연결 성공
18 +db.once('open', function()
19 +{
20 + console.log('Connected!');
21 +});
22 +
23 +
24 +// DB모델정의
25 +var Users = require('./models/users');
26 +
27 +// ejs사용
28 +// json사용설정
29 +app.set('view engine','ejs');
30 +app.use(bodyParser.json());
31 +app.use(bodyParser.urlencoded({extended: true}));
32 +
33 +// router import
34 +var router = require('./routing')(app, Users);
35 +
36 +var server = app.listen(23023, function()
37 +{
38 + var host = server.address().address;
39 + var port = server.address().port;
40 + console.log("http://%s:%s",host, port);
41 +});
...\ No newline at end of file ...\ No newline at end of file
1 +var mongoose = require('mongoose');
2 +
3 +var userSchema = mongoose.Schema
4 +(
5 + {
6 + id: String,
7 + pwd: String,
8 + name: String
9 + }
10 +);
11 +
12 +module.exports = mongoose.model('user',userSchema);
13 +
...@@ -39,4 +39,4 @@ TestModel.find(function(err, test){ ...@@ -39,4 +39,4 @@ TestModel.find(function(err, test){
39 }); 39 });
40 40
41 41
42 -//db.close()
...\ No newline at end of file ...\ No newline at end of file
42 +db.close()
...\ No newline at end of file ...\ No newline at end of file
......
1 +module.exports = function(app, Users)
2 +{
3 + app.get('/', function(req, res)
4 + {
5 + res.render("index");
6 + console.log("The index page!")
7 + });
8 +
9 + // 로그인 수행 - POST
10 + app.post('/login', function(req, res)
11 + {
12 + Users.find({id: req.body.id, pwd: req.body.pwd},{_id: 1}, function(err, user)
13 + {
14 + if(err)
15 + {
16 + console.log("Error!");
17 + res.send("Error!")
18 + }
19 +
20 + // 매칭정보 없음 - 로그인 실패
21 + if(user.length==0)
22 + {
23 + console.log("Login failed!")
24 + res.send("Login_failed");
25 + }
26 +
27 + // 매칭정보 있음 - 로그인 성공
28 + else
29 + {
30 + console.log("Login Success!")
31 + res.redirect("/main");
32 + // main으로 이동
33 + }
34 + });
35 + });
36 +
37 +
38 + // 메인화면 - 로그인 후 기본 검색화면
39 + app.get('/main', function(req,res)
40 + {
41 + res.render("main");
42 + console.log("The test page!")
43 + });
44 +
45 +
46 +
47 + // Join
48 + app.route('/join')
49 + .get(function(req, res) // 처음 Join화면 랜더 - GET
50 + {
51 + res.render("join")
52 + })
53 + .post(function(req, res) // 실제 Join 수행 - POST
54 + {
55 + // user정보 입력
56 + var user = new Users();
57 + user.id = req.body.id;
58 + user.pwd = req.body.pwd;
59 + user.name = req.body.name;
60 +
61 + // DB저장
62 + user.save(function(err)
63 + {
64 + if(err)
65 + {
66 + console.log(err);
67 + res.send("Error!")
68 + }
69 + else
70 + {
71 + console.log("Join Success");
72 + res.redirect('/');
73 + }
74 + });
75 + });
76 +
77 +
78 +
79 +}
...\ No newline at end of file ...\ No newline at end of file
1 +<div class="contents_index">
2 + <form method="POST" action="/login">
3 + <label>id:</label><input type="text" name="id"><br/>
4 + <label>pwd:</label><input type="password" name="pwd"><br/>
5 + <button type="submit">로그인</button>
6 + </form>
7 +</div>
...\ No newline at end of file ...\ No newline at end of file
1 +<div class="contents_index">
2 + <form method="POST" action="/join">
3 + <label>id:</label><input type="text" name="id"><br/>
4 + <label>pwd:</label><input type="password" name="pwd"><br/>
5 + <label>name:</label><input type="text" name="name"><br/>
6 + <button type="submit">가입</button>
7 + </form>
8 + <a href='/'><button>취소</button></a>
9 +</div>
...\ No newline at end of file ...\ No newline at end of file
1 +<div class="contents_main">
2 + <form method="POST" action="/join">
3 + <label>id:</label><input type="text" name="id"><br/>
4 + <label>pwd:</label><input type="password" name="pwd"><br/>
5 + <label>name:</label><input type="text" name="name"><br/>
6 + <button type="submit">가입</button>
7 + </form>
8 +</div>
...\ No newline at end of file ...\ No newline at end of file
1 +<html>
2 +<head>
3 + <title>Index</title>
4 +</head>
5 +<body>
6 + <% include ./navigation_index.ejs %>
7 + <% include ./contents_index.ejs %>
8 +</body>
9 +</html>
...\ No newline at end of file ...\ No newline at end of file
1 +<html>
2 +<head>
3 + <title>Join</title>
4 +</head>
5 +<body>
6 + <% include ./navigation_index.ejs %>
7 + <% include ./contents_join.ejs %>
8 +</body>
9 +</html>
...\ No newline at end of file ...\ No newline at end of file
1 +<html>
2 +<head>
3 + <title>Main</title>
4 +</head>
5 +<body>
6 + <% include ./navigation_main.ejs %>
7 + <% include ./contents_main.ejs %>
8 +</body>
9 +</html>
...\ No newline at end of file ...\ No newline at end of file
1 +<div class="navigation_index">
2 + <a href='/join'><button>회원가입</button></a>
3 + <a href='/'><button>Index</button></a>
4 +</div>
...\ No newline at end of file ...\ No newline at end of file
1 +<div class="navigation_main">
2 + <button>로그아웃</button>
3 + <button>검색</button>
4 + <button>마이페이지</button>
5 + <button>나만의시간표</button>
6 +</div>
...\ No newline at end of file ...\ No newline at end of file