-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·67 lines (56 loc) · 1.64 KB
/
server.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
63
64
65
66
67
#!/usr/bin/env node
// server.js
//===========
/*
* This is where all the magic happens.
* The xway dashboard calls this script as is
* `node server.js --port <free port number>`
* after that everyline here will be executed.
*
* You can install extra modules thanks to the work
* of npm. Also you can create a shell script to
* install any missing system package.
*/
/* Requires node.js libraries */
var http = require('http');
var fs = require('fs');
var exec = require('child_process').exec;
var argv = require('minimist')(process.argv.slice(2)); // must-have package
/* xyos apps need to accept the port to be launched by parameters */
port = argv.port;
if(isNaN(port)) {
console.log("Port \"" + port + "\" is not a number.");
process.kill(1);
} else {
console.log("Listening on port " + port);
exec('service vsftpd start', function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
http.createServer(function(request, response) {
/* index.html is an user interface example */
fs.readFile('index.html', 'utf8', function (error, data) {
if (error) {
return console.log(error);
}
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(data);
response.end();
});
}).listen(port);
function onExit() {
exec('service vsftpd stop', function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
} else {
process.exit(0);
}
});
}
process.on('SIGTERM', onExit);