forked from steeve/angular-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-seo-server.js
More file actions
63 lines (57 loc) · 1.96 KB
/
Copy pathangular-seo-server.js
File metadata and controls
63 lines (57 loc) · 1.96 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
var system = require('system');
if (system.args.length < 3) {
console.log("Missing arguments.");
phantom.exit();
}
// http://phantomjs.org/api/webserver/
var server = require('webserver').create();
var port = parseInt(system.args[1]);
var urlPrefix = system.args[2];
if (urlPrefix.indexOf('http') != 0) {
urlPrefix = "http://" + urlPrefix;
}
var renderHtml = function(url, cb) {
var page = require('webpage').create();
page.settings.loadImages = false;
page.settings.localToRemoteUrlAccessEnabled = true;
page.onCallback = function(data) {
// fired by window.callPhantom (fired by $scope.htmlReady() )
var status = data && data.status ? data.status : 200;
cb(page.content, status);
page.close();
};
page.viewportSize = {
width: 1200,
height: 1200
};
// This callback is invoked after the web page is created but before a URL is loaded.
page.onInitialized = function() {
page.evaluate(function() {
setTimeout(function() {
window.callPhantom();
}, 5000);
});
};
page.open(url);
};
// keepAlive: false is required since no Content-Length header is sent http://phantomjs.org/api/webserver/method/listen.html
server.listen(port, {'keepAlive': false}, function (request, response) {
// Route should be everything after the _escaped_fragment_=
var route = request.url.substring(request.url.indexOf('_escaped_fragment_=') + 19) || '';
var url = urlPrefix
+ request.url.slice(1, request.url.indexOf('?'))
+ '/#!';
if (route) {
url += decodeURIComponent(route);
}
// TODO: command line flag to turn this logging on or off
console.log(url);
renderHtml(url, function(html, statusCode) {
response.statusCode = statusCode;
response.setHeader('Content-Type', 'text/html');
response.write(html);
response.close();
});
});
console.log('Listening on ' + port + '...');
console.log('Press Ctrl+C to stop.');