forked from sbs20/scanservjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (82 loc) · 2.19 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var Config = require('./classes/Config');
var Api = require('./classes/Api');
var forbidden = function (req, res) {
res.status(403).send('<h1>Error 403: Forbidden</h1>');
};
var wrapError = function (err) {
return {
message: err.message,
code: err.code
};
};
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use('/', express.static('.'));
app.get('/node*', forbidden);
app.get('/api.js', forbidden);
app.get('/file', function (req, res) {
var api = new Api();
api.fileList().then(function (reply) {
res.send(reply);
});
});
app.delete('/file/*', function (req, res) {
var fullpath = req.params[0];
var api = new Api();
api.fileDelete({ data: fullpath}).then(function (reply) {
res.send(reply);
});
});
app.get('/ping', function (req, res) {
res.send('Pong@' + new Date().toISOString());
});
app.post('/convert', function (req, res) {
var api = new Api();
api.convert()
.then(function (fileInfo) {
fileInfo.content = fileInfo.toBase64();
res.send(fileInfo);
})
.fail(function (data) {
var err = wrapError(data);
res.status(500).send(err);
});
});
app.post('/scan', function (req, res) {
var param = req.body;
var api = new Api();
api.scan(param)
.then(function (data) {
res.send(data);
})
.fail(function (data) {
var err = wrapError(data);
res.status(500).send(err);
});
});
app.post('/preview', function (req, res) {
var param = req.body;
var api = new Api();
api.preview(param)
.then(function (data) {
res.send(data);
})
.fail(function (data) {
var err = wrapError(data);
res.status(500).send(err);
});
});
app.get('/diagnostics', function (req, res) {
var api = new Api();
api.diagnostics()
.then(function (tests) {
res.send(tests);
});
});
app.listen(Config.Port, function () { console.log('listening'); });