[英]How do you call a node.js function from a HTML page?
我正在關注 node.js 和 Azure 服務總線教程。 我能夠將以下內容作為節點應用程序運行,但是,我正在努力從我的 HTML 頁面調用節點函數:
請注意,所有文件都已正確加載節點 http-server 模塊,但是,當我調用 main 函數時,出現以下錯誤:
ReferenceError: ServiceBusClient is not defined
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);
});
非常感謝任何幫助。
總結 Pogrindis 的評論以供其他社區參考:
Node 是后端業務邏輯,HTML 是前端,因此與 Node.js 中的方法沒有直接通信。 我們可以實現一些像express
這樣的網絡服務器來允許對節點服務器進行 http 調用,然后我們可以從那里調用業務邏輯。 而對於服務總線的錯誤,我們需要實現ServiceBusClientOptions接口。
若要在網站上使用 Azure SDK 庫,需要將代碼轉換為在瀏覽器中工作。 您可以使用打包器(例如 rollup、webpack、parcel 等)執行此操作。請參閱此打包文檔以在瀏覽器中使用@azure/service-bus
庫。
此外,您示例中的代碼看起來使用的是版本 1。
最近發布了 7.0.0 版。 請參閱以下鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.