-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·133 lines (108 loc) · 3.25 KB
/
index.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
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
128
129
130
131
132
133
const Koa = require('koa');
const next = require('next');
const Router = require('koa-router');
const LRUCache = require('lru-cache');
const logger = require('koa-logger');
const provider = require('./src/middleware/config/config.js');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const mongoose = require('mongoose');
const serviceFactory = require('./src/middleware/services/ServiceFactory.js');
const ssrCache = new LRUCache({
max: 100,
maxAge: 1000 * 60 * 60 // 1hour
});
app.prepare()
.then(() => {
const server = new Koa();
const router = new Router();
// mongoose.connect('mongodb://localhost/test');
// const db = mongoose.connection;
// db.on('error', function(err) {
// console.error('error occurs:', JSON.stringify(err));
// mongoose.disconnect();
// });
// const userService = serviceFactory.create('User');
// userService.find({name: 'John'}, function(data) {
// console.log('user John has been found:', data);
// });
// db.once('open', function (callback) {
// // yay!
// console.log('yay');
// //const User = mongoose.model('User');
// // const userId = mongoose.Types.ObjectId();
// // const john = new User({
// // _id: userId,
// // name: 'John',
// // nickName: 'John Doe',
// // password: 'xxyyzz',
// // });
// // john.save(function (err, john) {
// // if (err) return console.error(err);
// // console.log('User john has been successfully saved...', john);
// // });
// });
// page routers
router.get('/index', function *() {
let { req, res, query } = this;
let path = '/index'
renderAndCache(req, res, path, query)
this.respond = false
})
router.get('/other', function *() {
let { req, res, query } = this;
let path = '/other'
renderAndCache(req, res, path, query)
this.respond = false
})
// interface routers
router.get('/user/:id', function *(id) {
let { req, res, params} = this;
res.send(500, {'error': 'holy shit'});
res.end(JSON.stringify({
username: 'John Doe ' + ~~(100 * Math.random()),
age: 45,
}))
})
router.get('*', function *() {
handle(this.req, this.res)
this.respond = false
})
server.use(logger())
server.use(function *(next) {
// Koa doesn't seems to set the default statusCode.
// So, this middleware does that
this.res.statusCode = 200
yield next
})
server.use(router.routes())
server.listen(3001, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3001')
})
})
function renderAndCache (req, res, path, query) {
let html
let cacheKey = getCacheKey(req)
if (html = ssrCache.get(cacheKey)) {
res.setHeader('Content-Type', 'text/html')
res.setHeader('Content-Length', Buffer.byteLength(html))
console.log(`Hit cache key ${cacheKey}`)
res.end(html)
return
}
app.renderToHTML(req, res, path, query)
.then((html) => {
console.log(`Miss cache ${path}`);
res.setHeader('Content-Type', 'text/html')
res.setHeader('Content-Length', Buffer.byteLength(html))
ssrCache.set(path, html)
res.end(html)
}).catch((e) => {
console.error(e)
})
}
function getCacheKey (req) {
return `${req.url}`
}