节点phantomjs seo通过节点运行写入文件

时间:2014-05-12 21:36:17

标签: node.js seo phantomjs

我想知道这是否是写作方式:

的node.js

var fs = require('fs');
var path = require('path');
var childProcess = require('child_process');
var phantomjs = require('phantomjs');
var binPath = phantomjs.path;

var childArgs = [
  path.join(__dirname, 'phantomjs-runner.js'),
  'http://localhost:3000'
]

childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
    if(err){

    }
    if(stderr){

    }
    fs.writeFile(__dirname+'/public/snapshots/index.html', stdout, function(err) {
        if(err) {
             console.log(err);
        } 
        else {
            console.log("The file was saved!");
        }
    }); 
});

phantomjs-runner.js

var system = require('system');
var url = system.args[1] || '';
if(url.length > 0) {
    var page = require('webpage').create(); 
    page.open(url, function (status) {
        if (status == 'success') {
            var delay, checker = (function() {
                var html = page.evaluate(function () {
                    var body = document.getElementsByTagName('body')[0];
                    if(body.getAttribute('data-status') == 'ready') {
                        return document.getElementsByTagName('html')[0].outerHTML;
                    }
                });
                if(html) {
                    clearTimeout(delay);
                    console.log(html);
                    phantom.exit();
                }
            });
            delay = setInterval(checker, 100);
        }
    });
}

可能是更好的方式,如

clearTimeout(delay);
fs.writeFile
phantom.exit();

我如何管理ie

不同的网址和不同的文件 我的意思是

http://localhost:3000 index.html 
http://localhost:3000/blog blog.html
http://localhost:3000/blog/postid postid.html

结束

'use strict';

var fs = require('fs'),
    path = require('path'),
    childProcess = require('child_process'),
    phantomjs = require('phantomjs'),
    binPath = phantomjs.path,
    config = require('../config/config');

var env = (process.env.NODE_ENV === 'production') ? 'production' : null,
    currentPath = (env) ? config.proot + '/build/default/snapshots' : config.proot + '/default/snapshots'; 

function normalizeUrl(url){
    if( (typeof url === 'undefined') || !url){
       return '';
    }
    if ( url.charAt( 0 ) === '/' ){
        url = url.substring(1);
    }
    return '/'+url.replace(/\/$/, "");
}

function normalizePage(route){
    if(!route){
       return 'index'; 
    }
    var chunks = route.substring(1).split('/');
    var len = chunks.length;
    if(len===1){
        return chunks[0];
    }
    chunks.shift();
    //I get the id
    return chunks[0];
}

module.exports = function (url) {
    var route = normalizeUrl(url);
    var page = normalizePage(route);
    var childArgs = [
        path.join(__dirname, './phantomjs-runner.js'),
        config.url+route
    ];
    childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
        if(err){

        }
        if(stderr){

        }
        fs.writeFile(currentPath + '/' + page + '.html', stdout, function(err) {
            if(err) {
                 console.log(err);
            } 
            else {
                console.log("The file was saved!");
            }
        }); 
    });
};

所以我为参数而努力了 等待获得输出的方式^^

0 个答案:

没有答案