可以在浏览器中使用节点包ssh2吗? [英] Can you use the node package ssh2 in a browser?

查看:52
本文介绍了可以在浏览器中使用节点包ssh2吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学校的浏览器中构建一个SFTP客户端.我正在使用节点包ssh2,并且能够使我的代码在终端上工作.我可以使用代码连接并读取该服务器中的目录和文件.但是,我无法通过浏览器中的HTML部分执行代码.我收到错误不支持绑定".我正在使用angular和browserify.因为ssh2包是服务器端的,所以我不得不使用browserify来在需要时在我的JavaScript文件的顶部要求"它.有没有人在浏览器中使用过ssh2软件包?我认为绑定错误消息告诉我此程序包仅在终端中可用,但我想确定一下.这是我的代码...

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...

控制器:

"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>

推荐答案

否.

您不能使用ssh2,因为它使用本机代码并且不能在浏览器中运行.它只能从node.js运行.

You cannot use ssh2 because it uses native code and cannot be run in the browser. It must be run from node.js only.

您最好的选择是使用可以在WebSocket上代理ssh的东西.

Your best bet would be to use something that can proxy ssh over a websocket.

这篇关于可以在浏览器中使用节点包ssh2吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆