forked from gwuhaolin/prerender-mongo-cache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (43 loc) · 1.38 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
/**
* Created by wuhaolin on 9/13/15.
*/
var mongoose = require('mongoose');
var PageSchema = new mongoose.Schema({
url: {type: String, index: true, unique: true},//主键当前页面的url
document: {type: String},//html文档内容
updatedAt: {type: Date, default: Date.now}//文档最后更新时间
});
var Page = mongoose.model('Page', PageSchema);
module.exports = {
init: function () {
mongoose.connect('mongodb://localhost/prerender');
},
beforePhantomRequest: function (req, res, next) {
if (req.method === 'GET') {
var url = req.url;
Page.findOne({url: url}).then(function (page) {
if (page) {
res.send(200, page.document);
} else {
next();
}
}, function () {
next();
});
} else {//如果不是GET请求 那么就不使用缓存机制 强制重新去抓取更新到缓存
next();
}
},
afterPhantomRequest: function (req, res, next) {
var page = {
url: req.url,
document: req.prerender.documentHTML,
updatedAt: Date.now()
};
Page.findOneAndUpdate({url: page.url}, page, {upsert: true}).then(function (page) {
}, function (err) {
console.error(err);
});
next();
}
};