在nodejs服务器端运行传单

时间:2021-03-11 19:38:05

I tried to run leaflet on a nodejs server side without success. I build it with jake as described in the download section, but then, when I require leaflet on a server file, if I start my node server, it crash with this error :

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

ReferenceError: window is not defined

Thanks node, I know it. But Is there a way to use leaflet on server side ? I need it for some operation on a L.geojson (https://github.com/mapbox/leaflet-pip) and I can't manage to do it without the "L" reference.

谢谢node,我知道了。但是有没有办法在服务器端使用传单呢?我需要它在L上做一些手术。geojson (https://github.com/mapbox/leaflet-pip),如果没有“L”引用,我无法做到这一点。

I'll appreciate any help. Thanks.

我要感谢任何帮助。谢谢。

2 个解决方案

#1


1  

Vanilla Leaflet does not work in node. I've made a wrapper here: https://github.com/jieter/leaflet-headless

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

#2


3  

You can load leaflet in node.js by simulating the browser:

你可以在节点中加载传单。通过模拟浏览器:

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

I used this in conjunction with Point in Polygon for Leaflet.

我把这个和多边形中的点结合在一起用在传单上。

#1


1  

Vanilla Leaflet does not work in node. I've made a wrapper here: https://github.com/jieter/leaflet-headless

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

#2


3  

You can load leaflet in node.js by simulating the browser:

你可以在节点中加载传单。通过模拟浏览器:

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

I used this in conjunction with Point in Polygon for Leaflet.

我把这个和多边形中的点结合在一起用在传单上。