如何在嵌入式JavaScript(EJS)代码中加载和使用模块

发布于 2025-01-21 19:05:20 字数 1450 浏览 1 评论 0 原文

我当前的脚本/代码流是 index.js-> doutes.js-> main.ejs 我需要加载与Geotiff相关的模块,并在我的 main中使用其绘图功能。 EJS 。但是,我发现 ejs 不支持代码中的加载模块,因此我需要将geotiff模块加载到我的 index.js.js routes.js 并将功能渲染到 main.ejs
我从Google找到了一些示例,并遵循了他们的方法,但不幸的是我无法使其起作用。错误信息不断显示 Geotiff相关的功能Main.ejs未定义

我尝试使用的模块的github网站在这里:

您在用法示例中可以看到,它直接需要 georaster,然后在相同的JS代码中构成绘图函数,而在我的情况下,我的绘图代码将在我的 main.ejs 中,其中我不能直接直接直接georaster。

遵循此页面中的方法:

我在 routes.js 中尝试了以下代码

var parse_georaster = require("georaster");
var GeoRasterLayer = require("georaster-layer-for-leaflet");

var getMain = function (req, res) {
   res.render('main',{parse_georaster: parse_georaster,GeoRasterLayer: GeoRasterLayer});
};

,但似乎不起作用。我仍然无法在我的 parse_georaster georasterlayer 的功能中使用我的 main.ejs

My current scripts/codes flow is index.js->routes.js->main.ejs I need to load a GeoTiff-related module and use its plotting functions in my main.ejs. However I find that ejs does not support loading modules within the code, so I need to load the GeoTiff modules in my index.js or routes.js and render the functions to main.ejs.
I found some examples from Google and followed their methods but unfortunately I couldn't make it work. The error information keeps showing that the GeoTiff-related functions in main.ejs are not defined.

The github website of the module I try to use is here:

As you can see in the Usage Example it directly require the georaster and then compose the plot functions in the same js code, whereas in my case my plot codes will be in my main.ejs where I can not require the georaster directly.

Following the method in this page:
https://rajuthapa-13222.medium.com/how-to-use-node-packages-on-ejs-html-templating-engine-d51885e91deb
I tried the following codes in my routes.js

var parse_georaster = require("georaster");
var GeoRasterLayer = require("georaster-layer-for-leaflet");

var getMain = function (req, res) {
   res.render('main',{parse_georaster: parse_georaster,GeoRasterLayer: GeoRasterLayer});
};

But it seems not working. I still can not use the function of parse_georaster and GeoRasterLayer in my main.ejs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文