加载外部javascript文件作为节点进程[重复]

问题描述 投票:0回答:1

我正在用google's API map编写purescript FFI绑定的集成测试。

问题Google的代码是在外部加载<script>标记,浏览器未下载并在节点进程中运行。我现在得到的将相关文件下载为gmaps.js,但我不知道如何实际运行该文件。

exports.setupApiMap = function() {
  require('dotenv').config();
  const apiKey = process.env.MAPS_API_KEY;
  const gmaps = "https://maps.googleapis.com/maps/api/js?key=" + apiKey;
  require('download')(gmaps, "gmaps.js");
  // what now???
  return;
};

对于我的单元测试,我必须以后才能运行new google.maps.Marker(...)。然后我可以检查我的setTitlegetTitle等绑定是否正常工作。

javascript node.js google-maps-api-3
1个回答
-2
投票

这是this one的重复问题。正确的代码是。

exports.setupApiMap = async function() {
  require('dotenv').config();
  const apiKey = process.env.MAPS_API_KEY;
  const gmaps = "https://maps.googleapis.com/maps/api/js?key=" + apiKey;
  await require('download')(gmaps, __dirname);
  const google = require('./js');
  return;
};

关键是在使用require之前下载到__dirname。这就是说我的具体用例没有用,因为谷歌的API地图代码不能在节点进程中运行。它必须在浏览器中运行。

© www.soinside.com 2019 - 2024. All rights reserved.