-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (84 loc) · 2.47 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"use strict";
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const graphqlHTTP = require('express-graphql');
const cors = require('cors');
const dao = require('./business/dao');
const graphql_schema = require('./graphql/schema')(dao);
const app = express();
app.use(express.static('public')); // public
app.use(session({
secret: 'letsstudygraphql!',
resave: false,
saveUninitialized: true,
})); // session
app.use(bodyParser.json()); // body parser
app.use(bodyParser.urlencoded({extended:false}));
/* cors */
const corsOptions = {
origin: true,
credentials: true
};
app.use(cors(corsOptions));
/* ejs */
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
/* graphql */
app.use('/graphql', graphqlHTTP(request => {
return {
schema: graphql_schema,
context: request.session,
graphiql: true,
}
}));
/* route */
app.get('/',function(req,res){
res.render('./index');
});
app.get('/join',function(req,res){
res.render('./join');
});
app.post('/join',function(req,res){
dao.join.joinUser(req.body.id, req.body.pwd)
.then(result => {
if(result.code !== 200) throw { name:'join failed', message:'something wrong' };
res.render('./index');
})
.catch(e => {
console.log(e.name+':'+e.message);
res.render('./error', {name: e.name, message: e.message});
});
});
app.get('/login',function(req,res){
if(req.session.user_id) return res.redirect('./board');
res.render('./login');
});
app.post('/login',function(req,res){
dao.login.doLogin(req.body.id, req.body.pwd)
.then(result => {
if(result.code !== 200) throw { name:'invalid information', message:'please check id, password' };
req.session.rid = result.rid; // user의 rid
req.session.user_id = req.body.id;
req.session.save(() => res.redirect('./board'));
})
.catch(e => {
console.log(e.name+':'+e.message);
res.render('./error', {name: e.name, message: e.message});
})
});
app.get('/board',function(req,res){
res.render('./board', {user_id:req.session.user_id});
});
app.get('/test',function(req,res){
//'select * from users where @rid = :rid'
dao.query('select * from board fetchplan *:1', {
params: {
rid: req.session.rid
}
}).then(result => console.log(result)); // check orient's metadata, properties
})
app.listen(3000,function(){
console.log(`Connect 3000 port!`)
});