-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeSCPFileTransfer.js
More file actions
64 lines (48 loc) · 1.65 KB
/
NodeSCPFileTransfer.js
File metadata and controls
64 lines (48 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// variable declaration
var path, node_ssh, ssh, fs;
// importing express server
var express = require('express');
var app = express();
fs = require('fs');
path = require('path');
// Module to connect to remote server
node_ssh = require('node-ssh');
ssh = new node_ssh();
// Password to connect to remote server
var password = '';
ssh.connect({
host: 'localhost', // remote server name or IP
username: 'username', // remote server username
port: 22,
password: password,
privateKey: '/home/pradeep/.ssh/id_rsa' // private key to connect to remote server (optional)
}).then(function () {
// Download the file from destination server
ssh.getFile('/home/pradeep/Pictures/sample.png', '/home/pradeep/Pictures/ip.png').then(function (Contents) {
console.log("The File's contents were successfully downloaded");
}, function (error) {
console.log("Something's wrong");
console.log(error);
});
});
// Readind the downloaded file from local machine
function read(file, callback) {
fs.readFile(file, function (err, imageData) {
if (err) console.log('Error:', err);
callback(null, imageData);
});
}
// Hosting the server
app.get('/', function (req, res) {
var data = null;
read('/home/pradeep/Pictures/sample.png', function (err, content) {
data = content;
// console.log(data);
});
// Send the file to the fronend to display or download
// res.download('/home/pradeep/Pictures/sample.png', 'sample.png');
res.contentType('image/png');
res.status(200).send(data, "binary");
});
app.listen(3000);
console.log('listing on port 3000');