带有 MongoDB NPM 模块的 CasperJS
CasperJS with MongoDB NPM Module
我是 运行 CasperJS 1.1.0-DEV 并且需要 mongoskin
NPM 模块将文档插入 Mongodb.
但是使用 mongoskin
NPM 模块
var mongo = require('mongoskin')
var db = mongo.db('mongodb://localhost:27017/test', {native_parser: true})')
抛出错误
ReferenceError: Can't find variable: process
/Users/username/casper-test/node_modules/mongoskin/index.js:1
/Users/username/casper-test/node_modules/mongoskin/index.js:2
TypeError: 'undefined' is not a function (evaluating 'mongo.db('mongodb://localhost:27017/test', {native_parser: true})')
test.js:3
如何将 CasperJS 与 mongoskin
这样的 NPM 模块一起使用的正确方法?
CasperJS 建立在 PhantomJS 之上,它本身具有与 node.js 不同的执行环境。 CasperJS 可以使用与 node.js 类似的模块基础设施,但大多数节点模块不能直接在 CasperJS 中 运行。由于 PhantomJS 不提供相同的基本模块,因此您不能使用使用它们的模块。可能无法重写 mongoskin 以使其使用 PhantomJS 模块。
另一种方法是编写一个使用 mongoskin 的 node.js 脚本,然后通过 child_process
module 使用该脚本调用节点。您需要将其与 CasperJS 中的控制流同步,因此您可以为此使用 casper.waitFor()
:
var execFile = require("child_process").execFile;
casper.callMongoskin = function(then, onTimeout, timeout){
var finished = false;
var results;
execFile("node", ["mongoskin_script.js"], null, function (err, stdout, stderr) {
console.log("execFileSTDOUT:", JSON.stringify(stdout));
console.log("execFileSTDERR:", JSON.stringify(stderr));
results = {stuff: "..."};
finished = true;
});
this.waitFor(function(){
return finished;
}, function _then(){
if (typeof then === "function") {
then.call(this, results);
}
}, onTimeout, timeout);
};
casper.thenCallMongoskin = function(then, onTimeout, timeout){
return this.then(function(){
this.callMongoskin(then, onTimeout, timeout);
});
};
我会让你弄清楚你想传递什么。下面是你如何使用它:
casper.start(url).thenCallMongoskin(function(results){
require('utils').dump(results);
}).run();
我是 运行 CasperJS 1.1.0-DEV 并且需要 mongoskin
NPM 模块将文档插入 Mongodb.
但是使用 mongoskin
NPM 模块
var mongo = require('mongoskin')
var db = mongo.db('mongodb://localhost:27017/test', {native_parser: true})')
抛出错误
ReferenceError: Can't find variable: process
/Users/username/casper-test/node_modules/mongoskin/index.js:1
/Users/username/casper-test/node_modules/mongoskin/index.js:2
TypeError: 'undefined' is not a function (evaluating 'mongo.db('mongodb://localhost:27017/test', {native_parser: true})')
test.js:3
如何将 CasperJS 与 mongoskin
这样的 NPM 模块一起使用的正确方法?
CasperJS 建立在 PhantomJS 之上,它本身具有与 node.js 不同的执行环境。 CasperJS 可以使用与 node.js 类似的模块基础设施,但大多数节点模块不能直接在 CasperJS 中 运行。由于 PhantomJS 不提供相同的基本模块,因此您不能使用使用它们的模块。可能无法重写 mongoskin 以使其使用 PhantomJS 模块。
另一种方法是编写一个使用 mongoskin 的 node.js 脚本,然后通过 child_process
module 使用该脚本调用节点。您需要将其与 CasperJS 中的控制流同步,因此您可以为此使用 casper.waitFor()
:
var execFile = require("child_process").execFile;
casper.callMongoskin = function(then, onTimeout, timeout){
var finished = false;
var results;
execFile("node", ["mongoskin_script.js"], null, function (err, stdout, stderr) {
console.log("execFileSTDOUT:", JSON.stringify(stdout));
console.log("execFileSTDERR:", JSON.stringify(stderr));
results = {stuff: "..."};
finished = true;
});
this.waitFor(function(){
return finished;
}, function _then(){
if (typeof then === "function") {
then.call(this, results);
}
}, onTimeout, timeout);
};
casper.thenCallMongoskin = function(then, onTimeout, timeout){
return this.then(function(){
this.callMongoskin(then, onTimeout, timeout);
});
};
我会让你弄清楚你想传递什么。下面是你如何使用它:
casper.start(url).thenCallMongoskin(function(results){
require('utils').dump(results);
}).run();