简体   繁体   English

如何从 HTML 页面调用 node.js 函数?

[英]How do you call a node.js function from a HTML page?

I'm following a node.js and Azure service bus tutorial.我正在关注 node.js 和 Azure 服务总线教程。 I'm able to run the below as a node app, however, I am struggling to call a node function from my HTML page:我能够将以下内容作为节点应用程序运行,但是,我正在努力从我的 HTML 页面调用节点函数:

Note that all the files have correctly loaded with the node http-server module, however, when I call the main function, I get the following error:请注意,所有文件都已正确加载节点 http-server 模块,但是,当我调用 main 函数时,出现以下错误:

ReferenceError: ServiceBusClient is not defined

Node.js function: Node.js 函数:

const { ServiceBusClient } = require("@azure/service-bus");

// Define connection string and related Service Bus entity names here
const connectionString ="";
const queueName = "";

async function main() {
  const sbClient = ServiceBusClient.createFromConnectionString(
    connectionString
  );
  const queueClient = sbClient.createQueueClient(queueName);
  const sender = queueClient.createSender();

  try {
    for (let i = 0; i < 1; i++) {
      const message = {
        body: "{}",
        label: "Contact",

        userProperties: {
          myCustomPropertyName: "my custom property value",
        },
      };
      console.log(`Sending message: ${message.body}`);
      await sender.send(message);
    }

    await queueClient.close();
  } finally {
    await sbClient.close();
  }
}

main().catch((err) => {
  console.log("Error occurred: ", err);
});

Any help much appreciated.非常感谢任何帮助。

Summarize the comments from Pogrindis for other communities reference:总结 Pogrindis 的评论以供其他社区参考:

Node is backend business logic, the HTML is the front end and so there is no direct communication to the methods in Node. Node 是后端业务逻辑,HTML 是前端,因此与 Node.js 中的方法没有直接通信。 We could implement some webserver like express to allow http calls to be made to the node server, and from there we could call the business logic.我们可以实现一些像express这样的网络服务器来允许对节点服务器进行 http 调用,然后我们可以从那里调用业务逻辑。 And for the error of service bus, we need to implement the ServiceBusClientOptions interface.而对于服务总线的错误,我们需要实现ServiceBusClientOptions接口。

To use Azure SDK libraries on a website, you need to convert your code to work inside the browser.若要在网站上使用 Azure SDK 库,需要将代码转换为在浏览器中工作。 You can do this using bundler such as rollup, webpack, parcel, etc. Refer to this bundling docs to use the @azure/service-bus library in the browsers.您可以使用打包器(例如 rollup、webpack、parcel 等)执行此操作。请参阅此打包文档以在浏览器中使用@azure/service-bus库。

Moreover, the code in your sample looks like it is using version 1.此外,您示例中的代码看起来使用的是版本 1。

Version 7.0.0 has been recently published.最近发布了 7.0.0 版。 Refer to the links below.请参阅以下链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM