首页 > 解决方案 > 如何为“react”连接“network-speed”库?

问题描述

给出错误“URL 不是构造函数”

我的代码https://codesandbox.io/s/patient-dust-do1d8?file=/src/App.js

https://www.npmjs.com/package/network-speed

import React from "react";

const NetworkSpeed = require('network-speed');
const testNetworkSpeed = new NetworkSpeed();

async function getNetworkDownloadSpeed() {
  const baseUrl = "http://eu.httpbin.org/stream-bytes/50000000";
  const fileSizeInBytes = 50000000;
  const speed = await testNetworkSpeed.checkDownloadSpeed(
    baseUrl,
    fileSizeInBytes
  );
  console.log(speed);
}

export default function App() {
  console.log(NetworkSpeed)
  return (
    <div className="App">
      <button onClick={() => getNetworkDownloadSpeed()}>34</button>
    </div>
  );
}

标签: reactjs

解决方案


你不能在 React 中使用那个包。

它不是为了在浏览器中使用,而是在 Node.js 中使用(其中一个是httphttps内置模块)。


推荐阅读