phantomjs create inside express Router does not execute



我有一个服务器,其中的路由是用express.Router构建的。其中一条路线在里面使用phantom.create()。我无法进入create函数,它永远不会执行(或者它执行但没有任何效果)。我通过以下方式运行此脚本node ./path/to/script

var express = require("express");
var router  = express.Router();
router.post("/my/path", function(req, res) {
console.log('req.body.myUrl', req.body.myUrl); // this works
var phantom = require('phantom');
phantom.create('--debug=true', '--web-security=false', function(ph) {
// this never happens:
console.log('phantom');
return res.status(200).send({ message: "asdasd" });
});
console.log('mamma mia') // this works
});

服务器记录以下内容:

req.body.myUrl http://myUrl
mamma mia
# and after a few minutes this:
POST /myUrl - - ms - -
# and then the entire cycle repeats infinitely automatically:
req.body.myUrl http://myUrl
mamma mia
POST /myUrl - - ms - -

当我在活动节点会话($ node)中运行相同的代码时,一切正常。知道这里有什么问题吗?我一直在挖掘create()源,但在那里找不到任何痕迹:/

节点 v6.2.0, Express 4.1.0, phantomjs 2.1.1, macOS Sierra 10.12.4,

我只用最新的 Phantom (4.0.1) 测试了您的代码,所以我不知道它是否适用于您正在使用的版本,但 v4 抛出了一个关于参数未正确传递的错误(它们应该作为数组传递)。

试试这个:

phantom.create([ '--debug=true', '--web-security=false' ], ...);

您需要在创建结束时添加一个捕获,以删除UnhandledPromiseRejectionWarning。

相关内容

最新更新