如何在Angular应用中调用节点(Node)函数

huangapple go评论76阅读模式
英文:

How to call node function in angular application

问题

Here's the translated code portion:

我在Node项目中有这段可用的代码

export function startWhatsapp() {
  const qrcode = require('qrcode-terminal');
  
  const { Client } = require('whatsapp-web.js');
  const client = new Client();
  
  client.on('qr', qr => {
      qrcode.generate(qr, { small: true });
  });
  
  client.on('ready', () => {
      console.log('客户端已准备好!');
  });
  
  client.initialize();
}

不过,您提到您需要在Angular应用程序中使用它,最佳实践是什么?如果您需要有关如何在Angular应用程序中使用此代码的建议,请告诉我。

英文:

I have this working code in node project

export function startWhatsapp(){
const qrcode = require('qrcode-terminal');

const { Client } = require('whatsapp-web.js');
const client = new Client();

client.on('qr', qr => {
    qrcode.generate(qr, {small: true});
});

client.on('ready', () => {
    console.log('Client is ready!');
});

client.initialize();
}

I need to use it in my angular app
what is the best practice to do it ?

答案1

得分: 1

你可以使用 node 作为 web 服务器,并使用 express 进行设置。这很容易设置,然后从 express 服务器公开一个简单的 API,然后使用 Angular 中的 HttpClient 调用该 API。

https://expressjs.com/

如果你想要更实时的功能,可以基于事件使用 socket.io 与 node express 服务器集成。

https://medium.com/@raj_36650/integrate-socket-io-with-node-js-express-2292ca13d891

你甚至可以在客户端上使用 socket.io,以便实现实时通信。

英文:

You can use node web server, setup with express. It is quite easy to setup, then expose a simple API from the express server and use HttpClient from Angular to call the API.

https://expressjs.com/

If you want it more real time, based on the event, you can even use socket.io with node express server.

https://medium.com/@raj_36650/integrate-socket-io-with-node-js-express-2292ca13d891

You can use socket.io even on client side, so the it allow real time communication.

huangapple
  • 本文由 发表于 2023年1月4日 16:39:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75002864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定