在 nodejs 服务器端运行传单

Running leaflet on nodejs server side

本文关键字:运行 服务器端 nodejs      更新时间:2023-09-26

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

ReferenceError: window is not defined

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

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

您可以通过模拟浏览器在 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');

我将其与传单的多边形点结合使用。

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