0

I try to develop an app to create user in mikrotik user manager via api. And I also tried JavaScript in the terminal by using following command:

$node script name.js

That is working and a user created. Then I tried to run that JavaScript by on click html button. Then JavaScript doesn't run and no user crated. Code follows:

<html>
<head>

 </head>
<body>
<button type = "button" onclick="conn();">Try it</button>
<script type="text/javascript">
var api = require('mikronode');
 var connection = new api('192.168.5.1','admin','xxxxxx');
 connection.connect(function conn() {

    conn.closeOnDone(true); // All channels need to complete before the connection will close.

    var actionChannel=conn.openChannel();
    // These will run synchronsously
    actionChannel.write(['/tool/user-manager/user/add','=username=tiran','=password=123456','=customer=admin']); // don't care to do anything after it's done.
    actionChannel.write(['/tool/user-manager/user/create-and-activate-profile','=customer=admin','=numbers=tiran','=profile=general']); // don't care to do anything after it's done.
    //actionChannel.write('/tool/user-manager/user/print',function(chan) {

       //chan.on('done',function(data) {
          //packets=api.parseItems(data);
          //packets.forEach(function(packet) {
              //alert('done');
             //alert('user: '+JSON.stringify(packet));
              //console.log('user: '+JSON.stringify(packet));
          //});
          //listenChannel.close(); // This should call the /cancel command to stop the listen.
       //});
    //});
    actionChannel.close(); // The above commands will complete before this is closed.
 });
</script>
 </body>
 </html>
tiran
  • 21
  • 1
  • 4
  • You could do it using browserify see here: https://stackoverflow.com/questions/49562978/how-to-use-npm-modules-in-browser-is-possible-to-use-them-even-in-local-pc – Xsmael Dec 14 '22 at 09:42

1 Answers1

2

That code is for NodeJS, you cannot run in directly on the browser as JavaScript

Gabrielkdc
  • 105
  • 1
  • 11