forked from meshaabi/angular-seo
/
angular-seo-server.js
55 lines (44 loc) · 1.58 KB
/
angular-seo-server.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
var system = require('system');
if (system.args.length == 3) {
console.log("NOTE: Running in single site mode, snapping only "+system.args[2]);
} else if (system.args.length == 2) {
console.log("NOTE: Running in Nginx mode, snapping urls in Host header");
} else {
console.log("Missing arguments.");
phantom.exit();
}
var server = require('webserver').create();
var port = parseInt(system.args[1]);
var renderHtml = function(url, cb) {
var page = require('webpage').create();
page.settings.loadImages = false;
page.settings.localToRemoteUrlAccessEnabled = true;
page.onCallback = function() {
cb(page.content);
page.close();
};
// page.onConsoleMessage = function(msg, lineNum, sourceId) {
// console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
// };
page.onInitialized = function() {
page.evaluate(function() {
setTimeout(function() {
window.callPhantom();
}, 10000);
});
};
page.open(url);
};
server.listen(port, function (request, response) {
var host = request.headers.Host;
var urlPrefix = (typeof system.args[2] == 'undefined') ? 'http://' + host : system.args[2];
var route = request.url.replace("?_escaped_fragment_=","#");
var url = urlPrefix + decodeURIComponent(route);
renderHtml(url , function(html) {
response.statusCode = 200;
response.write(html);
response.close();
});
});
console.log('Listening on ' + port + '...');
console.log('Press Ctrl+C to stop.');