-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.js
49 lines (40 loc) · 945 Bytes
/
main.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
var express = require('express')
var fs = require('fs')
var app = express()
var args = process.argv.slice(2);
var CMD = args[0] || "test";
var PORT = args.length == 2? args[1] : 9001;
var id = Math.floor(Math.random()*1000);
app.get('/', function(req, res)
{
res.send("Hi From "+id);
});
function start()
{
return new Promise(function(resolve,reject)
{
server = app.listen(PORT, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
resolve({host: host, port: port});
}).on('error', function (err) {
if(err.errno === 'EADDRINUSE') {
console.log(`----- Port ${port} is busy, try with another port`);
} else {
console.log(err);
}
});
});
}
function stop()
{
return server.close();
}
(async () => {
if( CMD === "start" )
{
await start();
}
})();
module.exports = { start: start, stop: stop};