使用Phantom和node,将网页变成图片,放在函数里无效

在utils文件里有phantom.js和capture.js两个文件phantom.js
我调用/send接口,希望执行phantom.js里面的功能,
但是没有效果,请问是什么原因?

const spawn = require('child_process').spawn;

let Phantom = {
    createImage: ()=> {
        // 捕获标准输出并将其打印到控制台

        return new Promise(function (resolve, reject) {
            const process = spawn('phantomjs',[ __dirname + '/capture.js']);
            // console.log(processor.cwd());

            process.stdout.on('data', function (data) {
                console.log('标准输出:\n' + data);
                resolve(data);
            });

// 捕获标准错误输出并将其打印到控制台
            process.stderr.on('data', function (data) {
                console.log('标准错误输出:\n' + data);
            });

// 注册子进程关闭事件
            process.on('exit', function (code, signal) {
                console.log('子进程已退出,代码:' + code);
            });
        });
    },
};

module.exports = Phantom;

capture.js

var page=require('webpage').create();//创建一个网页对象;
var system=require('system');
var address,fileName;
// page.viewportSize={width:1024,height:800};//设置窗口的大小为1024*800;
// page.clipRect={top:0,left:0,width:1024,height:800};//截取从{0,0}为起点的1024*800大小的图像;
// //禁止Javascript,允许图片载入;
// // 并将userAgent改为"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) PhantomJS/19.0";
// page.settings={
//     javascriptEnabled: false,
//     loadImages: true,
//     userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) PhantomJS/19.0'
// };


var page = require('webpage').create();
page.open('https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&word=phantom+open', function() {
    page.render('baidu.png');
    phantom.exit();
});

在另外的routes文件夹里有poster.js

/**
 * 租房信息
 */
const app = require('express').Router();
const db = require('../../modules/database');
const helper = require('../../utils/helper');
const config = require('../../modules/configuration');
const phantom = require('../../modules/utils/phantom');

/**
 * 更新租房信息
 */
app.all('/send', (req, res) => {
    
    let returnResult = {};


    phantom.createImage().then(data=>{
        console.log(data)
    });



});

module.exports=app;
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题