简体   繁体   English

如何在nodejs中使用第三方库

[英]How to use a third party library in nodejs

I am trying to setup a webserver that is able to send messages to a message queue.我正在尝试设置一个能够将消息发送到消息队列的网络服务器。 For this, I want to use STOMPIT .为此,我想使用STOMPIT I have setup a basic nodejs webserver with an index.html, main.js and app.js but can't get the stompit library working in my main.js.我已经设置了一个带有 index.html、main.js 和 app.js 的基本 nodejs 网络服务器,但无法让 stompit 库在我的 main.js 中工作。

How can I use the library (imported with the script tags in the html) in my main.js?如何在我的 main.js 中使用库(使用 html 中的脚本标签导入)?

Parts of the code: index.html:部分代码:index.html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.js"></script>
<script src="js/main.js"></script>

main.js:主.js:

    stompit.connect(connectOptions, function(error, client) { //stompit is undefined here

        if (error) {
            console.log('connect error ' + error.message);
            return;
        }

        const sendHeaders = {
            'destination': '/queue/test',
            'content-type': 'text/plain'
        };

        const frame = client.send(sendHeaders);
        frame.write(msg);
        frame.end();
    })

You have to choose a module on npmjs.com, install it and use it in your scripts.你必须在 npmjs.com 上选择一个模块,安装它并在你的脚本中使用它。 For example with stomp module (there are others on npmjs): https://www.npmjs.com/package/stompjs例如,使用 stomp 模块(npmjs 上还有其他模块): https://www.npmjs.com/package/stompjs

Install it with:安装它:

npm install --save stompjs

Import it in your scripts:将其导入您的脚本:

const Stomp = require('stompjs');

You can find other details about using it on: https://www.npmjs.com/package/stompjs您可以在以下位置找到有关使用它的其他详细信息: https://www.npmjs.com/package/stompjs

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

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