在nodejs服务器端运行传单

Thc*_*hco 5 javascript node.js leaflet

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

ReferenceError: window is not defined
Run Code Online (Sandbox Code Playgroud)

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

我会感激任何帮助.谢谢.

Bre*_*Nee 8

您可以通过模拟浏览器在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');
Run Code Online (Sandbox Code Playgroud)

我将它与Point in Polygon for Leaflet结合使用.

  • 您还应该在`window`中添加`devicePixelRatio:1`,在`navigator`下添加`platform:'nodejs'`,以便正确加载Leaflet的新版本. (2认同)

Jie*_*ter 2

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