forked from gagle/node-tftp
/
no-pipe.js
62 lines (51 loc) · 1.31 KB
/
no-pipe.js
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
"use strict";
var fs = require ("fs");
var tftp = require ("../../lib");
/*
You don't need to pipe from a file, the response it's just a writable stream.
*/
var server = tftp.createServer ({
port: 1234,
denyPUT: true
}, function (req, res){
req.on ("error", function (error){
//Error from the request
console.error (error);
});
if (req.file === "hello"){
res.setUserExtensions ({ platform: process.platform, pid: process.pid });
var message = "Hello World!";
res.setSize (message.length);
res.end (message);
}else{
req.abort ("Can only GET the file 'hello'");
}
});
server.on ("error", function (error){
//Errors from the main socket
console.error (error);
});
server.on ("listening", doRequest);
server.listen ();
function doRequest (){
var options = {
userExtensions: {
platform: "",
pid: ""
}
};
tftp.createClient ({ port: 1234 }).createGetStream ("hello", options)
.on ("error", function (error){
server.close ();
console.error (error);
})
.on ("stats", function (stats){
console.log ("TFTP server running on " + stats.userExtensions.platform +
" with pid " + stats.userExtensions.pid + ".");
})
.on ("end", function (){
server.close ();
})
//Hello World!
.pipe (process.stdout);
}