-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (72 loc) · 2.74 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
var express = require('express');
var useragent = require('express-useragent');
var childProcess = require('child_process');
var lodash = require('lodash');
var path = require('path');
var urlencode = require('urlencode');
var phantomjs = require('phantomjs-prebuilt');
var app = express();
app.use(useragent.express());
//serving static folder
app.use('/bundle', express.static(path.join(__dirname, '/bundle')));
app.use('/client/app/css', express.static(path.join(__dirname, '/client/app/css')));
app.use('/client/app/js', express.static(path.join(__dirname, '/client/app/js')));
app.use('/client/app/css/images', express.static(path.join(__dirname, '/client/app/css/images')));
app.get('/*', function (req, res, next) {
var agent = req.useragent.source;
//checking bot here. If it is a bot then render via phantom
if ((lodash.includes(agent, 'Google') || lodash.includes(agent, 'facebookexternalhit') || lodash.includes(agent, 'Facebot')) && req.useragent.isPhantomJS === false) {
//sent request to render via phantom
return renderHtmlPhantom(req, res, next);
} else {
//sent request to render at cient browser
return next();
}
});
//default serving index.html file
app.get('/*', function (req, res, next) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(3000, function () {
console.log('application is listening on port 3000');
});
// phantom task here we are rendering a page using phantom js browser. url has
// been set by BOT;
function renderHtmlPhantom(req, res, next) {
var url = 'http://localhost:3000' + urlencode.decode(req.url, 'gbk');
console.log('-----BOT REQUEST URL----', url);
var agent = req.useragent.source;
//requesting phantom instance for redering the web page
phantomInstance(url, function (err, result) {
if (err) {
res
.status(404)
.send('Internal server error');
} else {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(result);
res.end();
}
});
}
// creating a phantom instance using phantom script file. more information can
// be found on phantomjs official page
function phantomInstance(url, callback) {
var binPath = phantomjs.path;
var childArgs = [
path.join(__dirname, 'phantom-script.js'),
'--disk-cache=false',
'--load-images=false',
url
]
childProcess.execFile(binPath, childArgs, {
maxBuffer: 500 * 1024
}, function (err, stdout, stderr) {
if (err) {
return callback(err);
}
return callback(null, stdout);
});
}