Tuesday 14 March 2017

Can you use the node package ssh2 in a browser?

I am building an SFTP client in my browser for school. I am using the node package ssh2 and am able to get my code to work in the terminal. I can use the code to connect and read the directories and files in that server. However, I am unable to execute the code through an HTML partial in my browser. I am receiving the error "binding is not supported". I am using angular and browserify. Because the ssh2 package is server-side, I had to use browserify in order to "require" it at the top of my JavaScript files where needed. Has anyone ever used the ssh2 package inside of their browser? I'm thinking that the binding error message is telling me that this package will only work in the terminal, but I wanted to be certain. Here is my code...

controller:

"use strict";

module.exports = function($scope) {

        $scope.connect = function(){
                let Client = require('../../lib/node_modules/ssh2-sftp-client');
                let sftp = new Client();
                sftp.connect({
                    host: 'test.rebex.net',
                    port: '22',
                    username: 'user',
                    password: 'password'
                }).then(() => {
                        var remoteServerFolders = sftp.list('/');
                        console.log("remoteServerFolders", remoteServerFolders);
                    return remoteServerFolders;
                }).then((data) => {
                    console.log('the data info', data);
                    sftp.end();
                }).catch((err) => {
                    console.log('catch error', err);
                });
        };
};

app.js:

"use strict";

let angular = require("../lib/node_modules/angular/");
let app = angular.module("SftpApp", ['ngRoute']);

require("../lib/node_modules/angular-route/angular-route.min.js");

require("./factories/");
require("./controllers/");

app.config(function($routeProvider){
        $routeProvider.
        when('/', {
                templateUrl: 'partials/connect.html',
                controller:"ConnectingCtrl"
        });
});

HTML:

<h1>Connect to an SFTP server</h1>

<div ng-controller="ConnectingCtrl">
        <button type="button" class="btn btn-default" ng-click="connect()">Connect</button>
</div>


via taltoooldtocode

No comments:

Post a Comment