forked from OmniLayer/omniwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve-static.js
71 lines (59 loc) · 1.59 KB
/
serve-static.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
68
69
70
71
var connect = require('connect'),
express = require( 'express' ),
http = require('http'),
url = require( 'url' ),
qs = require( 'qs' ),
fs = require( 'fs' )
var postPaths = [
'/v1/user/wallet/sync',
'/v1/user/wallet/restore/',
'/v1/address/addr/'
];
var app = express();
app.configure( function() {
app.use(connect.static('www'))
} );
postPaths.forEach( function( path ){
app.post( path, processPost );
} );
app.listen(3000);
function processPost( req, res ) {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var POST = qs.parse(body);
// use POST
if( req.route.path.indexOf( 'address/addr' ) >= 0 )
{
var file = "www" + req.route.path + POST.addr + '.json';
console.log( 'Get file: ' + file );
returnPath( file, res );
}
else if ( req.route.path.indexOf( 'wallet/restore' ) >= 0 )
{
var file = "www" + req.route.path + POST.email;
returnPath( file, res );
}
else
{
console.log( 'Unknown request: ' + req.route.path );
console.log( POST );
}
});
}
function returnPath( file, res ) {
fs.readFile( file, function (err, data) {
if (err || !data)
{
res.status( 500 );
res.end();
}
else
{
var obj = JSON.parse( data.toString( 'utf8' ));
res.json( obj );
}
});
}