index.js
2.96 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const http2 = require('http2');
var fs = require('fs');
const api = require('./api');
const Koa = require('koa');
const Router = require("koa-router");
const jwtMiddleware = require('./lib/jwtMiddleware');
require('dotenv').config();
const mongoose = require('mongoose');
const bodyParser = require('koa-bodyparser');
const port = process.env.PORT || 3000
//443
const passport = require('koa-passport')
const app = new Koa();
const router = new Router()
const render = require('koa-ejs');
const path = require('path');
//const User = require('../models/user');
//const views = require('koa-views');
const book = require('./routes/book');
const auth = require('./routes/user');
const page = require('./routes/page');
const send = require('koa-send');
var options = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt'),
allowHTTP1: true
};
//app.use(express.static('images'))
mongoose.Promise = global.Promise; // Node 의 네이티브 Promise 사용
// mongodb 연결
mongoose.connect(process.env.MONGO_URI).then(
(response) => {
console.log('Successfully connected to mongodb');
}
).catch(e => {
console.error(e);
});
//app.use(router.routes())
/*
app
.use(router.routes())
.use(router.allowedMethods());
render(app, {
root: path.join(__dirname, 'views'),
layout: false,
viewExt: 'ejs',
cache: false,
debug: true
});
*/
// app.use(async function (ctx) {
// await ctx.render('home/welcome');
// });
app
.use(jwtMiddleware)
.use(bodyParser()); // bodyParser는 라우터 코드보다 상단에 있어야 합니다.
render(app, {
root: path.join(__dirname, 'views'),
layout: false,
viewExt: 'ejs',
cache: false,
debug: true
});
app
.use(router.routes())
.use(router.allowedMethods())
.use(passport.initialize());
// .use(views(path.join(__dirname, 'views'), {
//extension: 'ejs'
//}))
//.use(views('views', { map: { html: 'ejs' } }));
router.get('/', async ctx =>{
await ctx.render('home/welcome');
});
router.get('/about', async ctx =>{
await ctx.render('home/about');
});
router.get('/auth', async ctx =>{
await ctx.render('users/login');
});
//router.use('/', api.routes());
//router.use("/book", require("./routes/book"));
//router.use("/page", require("./routes/page"));
//router.use("/auth", require("./routes/user"));
router.use('/auth', auth.routes());
router.use('/book', book.routes());
router.use('/page', page.routes());
app.use(router.routes()).use(router.allowedMethods());
//router.get('/', async (ctx, next) => {
// const rawContent = fs.readFileSync('index.html').toString('utf8')
//ctx.body = rawContent
//})
//router.use(api.routes());
//app.use(router.routes()).use(router.allowedMethods())
//app.use(router.routes()).use(router.allowedMethods());
http2
.createSecureServer(options, app.callback())
.listen(port, () => console.log("listening on port %i", port));
/*
app.listen(port, function () {
console.log('server listening on port %d', port);
});
*/