-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (62 loc) · 1.75 KB
/
Copy pathindex.js
File metadata and controls
84 lines (62 loc) · 1.75 KB
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
72
73
74
75
76
77
78
79
80
81
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var express = require('express');
var getRawBody = require('raw-body');
var config = require('./config');
var driver = require('./driver');
config.init('./state');
var app = express();
var port = process.env.PORT || 3015;
var local_addr = process.env.BINDIP || '127.0.0.1';
var router = express.Router();
app.use(function (req, res, next) {
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8',
}, function (err, string) {
if (err) return next(err)
try {
req.body = JSON.parse(string);
} catch (e) {
req.body = null;
}
next();
});
});
app.use(function (req, res, next) {
console.log('');
console.log('url: ', req.url);
console.log('body: ', req.body);
next()
});
app.use(function (req, res, next) {
var json = res.json;
res.json = function () {
console.log('res: ', arguments[0]);
json.apply(res, arguments);
};
next();
});
Object.keys(driver.call_map).forEach(function (call) {
var func = driver.call_map[call];
app.post(call, function(req, res) {
// console.log("REQ:", req.body);
func(req.body)
.then(function (res_data) {
// console.log("RES:", res_data);
res.json(res_data);
})
.catch(function (err) {
console.log("ERR:", err);
res.json({Err: err.toString()});
});
});
});
app.use(function (req, res, next) {
res.status(404).json({});
})
app.listen(port, function () {
console.log('started ' + port);
});