运行 nodejs 服务器端的传单

Running leaflet on nodejs server side

我尝试 运行 nodejs 服务器端的传单但没有成功。我按照下载部分中的描述使用 jake 构建它,但是,当我在服务器文件上需要传单时,如果我启动我的节点服务器,它会崩溃并出现此错误:

ReferenceError: window is not defined

感谢节点,我知道了。但是有没有办法在服务器端使用传单?我需要它在 L.geojson (https://github.com/mapbox/leaflet-pip) 上进行一些操作,如果没有 "L" 参考,我无法做到这一点。

如有任何帮助,我将不胜感激。 谢谢

Vanilla Leaflet 在节点中不起作用。我在这里做了一个包装器:https://github.com/jieter/leaflet-headless

您可以通过模拟浏览器在node.js中加载传单:

// Create globals so leaflet can load
global.window = {
  screen: {
    devicePixelRatio: 1
  }
};
global.document = {
  documentElement: {
    style: {}
  },
  getElementsByTagName: function() { return []; },
  createElement: function() { return {}; }
};
global.navigator = {
  userAgent: 'nodejs',
  platform: 'nodejs'
};
global.L = require('leaflet');

我将其与 Point in Polygon for Leaflet 结合使用。