-
Notifications
You must be signed in to change notification settings - Fork 0
/
scController.js
60 lines (56 loc) · 2.11 KB
/
scController.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
var http = require('http');
var url = require('url');
var fs = require('fs');
var qs = require('querystring');
var serviceModule1 = require('./getContractSource');
var serviceModule2 = require('./viewContractStatus');
var serviceModule3 = require('./transferTokens');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
if(filename == "./") filename += "index.html";
fs.readFile(filename, function(err, data) {
if(!err) {
res.write(data);
return res.end();
}
if(req.method == 'GET') {
var query = q.query;
if(q.pathname == "/contract/source") {
res.writeHead(200);
var msg = serviceModule1.getContractSource(query.c_name);
return res.end(msg);
}
else{
res.writeHead(400);
return res.end();
}
}
else if(req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var backHTML = "<p><a href='/'>BACK to index</a></p>";
var post = qs.parse(body);
if(q.pathname == "/contract/status") {
res.writeHead(200, {'Content-Type': 'text/html'});
var msg = serviceModule2.viewContractStatus(post['src_addr'], post['passphrase']) + backHTML;
return res.end(msg);
}
else if(q.pathname == "/contract/token/transfer") {
res.writeHead(200, {'Content-Type': 'text/html'});
var msg = serviceModule3.transferTokens(post['src_addr'], post['passphrase'],
post['amount'], post['dst_addr']) + backHTML;
return res.end(msg);
}
else{
res.writeHead(400);
return res.end();
}
});
}
else{}
});
}).listen(8888);