我对node.js非常非常新,我很可能会问一个非常简单的问题。
客户方:我已经能够通过d3js根据自定义./js/wikiatlas.js
(maps on client side)做出好here。
服务器端:我现在想要移动这个地图生成服务器端。我使用node,jsdom和d3js成功输出了一个基本的square.svg。我现在想要使用./js/wikiatlas.js
中更复杂的函数。所以我天真地添加了./js/wikiatlas.js
依赖项和js函数调用locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
期望它可以工作(其他的东西确实有效)。我的完整代码:
var jsdom = require('jsdom');
var fs = require('fs');
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ '../js/jquery-2.1.3.min.js',
'../js/d3.v3.min.js',
'../js/topojson.v1.min.js',
'../js/wikiatlas.js', // <<======== my code (IMPORTANT !!)
'../js/b64.js'], // ... & offline
//'http://rugger-demast.codio.io/js/wikiatlas.js',
function (err, window) {
/* COLLECT ENV.VARIABLES ******************************************* */
var WEST = process.env.WEST,
NORTH = process.env.NORTH,
EAST = process.env.EAST,
SOUTH = process.env.SOUTH,
target= process.env.ITEM,
title = process.env.ITEM,
WIDTH = process.env.WIDTH;
/* D3js FUNCTION *************************************************** */
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH); // <<======== defined in wikiatlas.js!
/* SVG PRINT ******************************************************* */
var svgheader = '<?xml version="1.0" encoding="utf-8"?>'
+'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
fs.writeFileSync('administrative_map_(wikiatlas_2014).svg', svgheader + window.d3.select("body").html());
// console.log(window.d3.select("body").html());
}
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);
但我接着发现ReferenceError: locationMap is not defined
错误:
/data/yug/projects_active/make-modules/09_d3/location.node.js:30
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
^
ReferenceError: locationMap is not defined
at Object.done (/data/yug/projects_active/make-modules/09_d3/location.node.js:30:1)
at /data/yug/projects_active/make-modules/node_modules/jsdom/lib/jsdom.js:270:18
at process._tickCallback (node.js:419:13)
make: *** [output] Error 8
我应该如何加载或构建./js/wikiatlas.js
以便我可以在node.js / jsdom服务器脚本中使用其中的函数?
似乎与:In Node.js, how do I "include" functions from my other files?,Nodejs script fails to print after d3.json()?
相关答案 0 :(得分:1)
由于您将脚本加载到窗口的上下文中,您还必须使用窗口访问它,即window.locationMap
。
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ '../js/jquery-2.1.3.min.js',
'../js/d3.v3.min.js',
'../js/topojson.v1.min.js',
'../js/wikiatlas.js', // <<======== my code (IMPORTANT !!)
'../js/b64.js'],
然后
window.locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
是的。