Advertisement
Capaj

dem2cz nodejs

Jan 2nd, 2013
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Module dependencies.
  3.  */
  4.  
  5. var express = require('express'),
  6.     //routes = require('./routes'),
  7.     phantomProxy = require('phantom-proxy'),
  8.     fs = require('fs'),
  9.     path = require('path'),
  10.     api = require('./routes/api');
  11.  
  12. var app = module.exports = express();
  13.  
  14. // Configuration
  15.  
  16. app.configure(function(){
  17.   app.set('env', 'production');
  18.   //app.use(express.logger());
  19.   app.use(express.bodyParser());
  20.   app.use(express.methodOverride());
  21.   app.use(app.router);
  22. });
  23.  
  24. app.configure('development', function(){
  25.   app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  26. });
  27.  
  28. app.configure('production', function(){
  29.   app.use(express.errorHandler());
  30. });
  31.  
  32. // Routes
  33. //app.get('/', routes.index);
  34. //app.get('/partials/:name', routes.partials);
  35. var appRoutes = ["/", "/home", "/contact", "/about"];
  36.  
  37. phantomProxy.create({}, function (proxy) {
  38.     app.get('*', function(req, res){
  39.         var agent = req.headers["user-agent"];
  40.         //console.log(req.ip + " sended a request for URL "+req.url+" -with user-agent " + agent);
  41.         var filePath = './public' + req._parsedUrl.pathname;
  42.         fs.exists(filePath, function (exists) {
  43.            if(exists){
  44.                res.sendfile(filePath);
  45.            }else{
  46.                if(typeof agent === "string"){
  47.                    if(agent.indexOf('Googlebot') === -1){   //user is not googlebot
  48.                        if(appRoutes.indexOf(req._parsedUrl.pathname) !== -1){
  49.                            // redirect all others to the index (client app is then responsible for handling it's route)
  50.                            res.sendfile('./public/index.html');
  51.                        }else{
  52.                            res.send(404);
  53.                            console.log('File '+filePath+' doesnt exist');
  54.                        }
  55.                    }else{
  56.                        console.log('Request sent by crawler, will start phantom');
  57.  
  58.                        var page = proxy.page;
  59.                        page.open('http://localhost'+ req._parsedUrl.pathname , function () {
  60.                            page.waitForSelector('#main *', function () {
  61.                                console.log('main div children present');
  62.                                var pageContent = null;
  63.                                page.evaluate(function () {
  64.                                    return document.getElementsByTagName('html')[0].innerHTML;
  65.                                }, function(ret){
  66.                                    pageContent = ret;
  67.                                    console.log("PhantomJS callback has been fired");
  68.  
  69.                                    res.send(pageContent);
  70.                                    res.end();
  71.  
  72.                                });
  73.  
  74.  
  75.                            });
  76.                        });
  77.                    }
  78.                }
  79.  
  80.            }
  81.         });
  82.     });
  83.     process.on('exit', function() {
  84.         console.log('About to exit, we shall close the phantom process.');
  85.         proxy.end(function(){});
  86.     });
  87. });
  88. // Start server
  89.  
  90. app.listen(80, function(){
  91.   console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
  92. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement