-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (95 loc) · 2.83 KB
/
Copy pathserver.js
File metadata and controls
112 lines (95 loc) · 2.83 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
// ExpressJS调用方式
var express = require('express');
var bodyParser = require('body-parser');
var redis = require("redis");
// 1 day
var defaultRedisEx = 86400;
var client = redis.createClient();
client.on("error", function (err) {
console.log("redis Error " + err);
});
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
// 引入NodeJS的子进程模块
var child_process = require('child_process');
app.get('/', function (req, res) {
// 完整URL
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('welcome to prerender!');
});
app.get('/home/*', function (req, res) {
// 完整URL
var url = req.protocol + '://' + req.hostname + req.url;
client.get(url, function(err, reply) {
if(err) {
console.log("error" + err);
} else {
if(reply == null){
console.log("rendering page#" + url);
render(url, res);
} else {
console.log("get cached page#" + url);
res.send(reply);
}
}
});
});
app.post('/api/refresh', function (req, res) {
// 更新cache
var url = req.body.url;
render(url, res);
// 完整URL
// res.writeHead(200, { 'Content-Type': 'text/plain' });
// res.end('/api/refresh');
});
function render(url,res){
console.log(url);
// 预渲染后的页面字符串容器
var content = '';
// 开启一个phantomjs子进程
var phantom = child_process.spawn('phantomjs', ['spider.js', url]);
// 设置stdout字符编码
phantom.stdout.setEncoding('utf8');
// 监听phantomjs的stdout,并拼接起来
phantom.stdout.on('data', function(data){
content += data.toString();
});
// 监听子进程退出事件
phantom.on('exit', function(code){
switch (code){
case 1:
console.log('loading failed#' + url);
res.send('loading failed');
break;
case 2:
console.log('loading timeout: '+ url);
res.send('loading timeout');
break;
default:
// 处理成功后,才渲染并设置缓存
client.set(url, content, 'EX', defaultRedisEx);
res.send(content);
break;
}
});
}
// 定制404页面
app.use(function(req, res){
res.type('text/plain');
res.status(404);
res.send('404 - Not Found');
});
// 定制 500 页面
app.use(function(err, req, res, next){
console.error(err.stack);
res.type('text/plain');
res.status(500);
res.send('500 - Server Error');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Server started on http://%s:%s', host, port);
});