This repository has been archived by the owner on Jan 26, 2019. It is now read-only.
forked from rumpelstiel/node-file-search-indexer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webService.js
75 lines (61 loc) · 2.17 KB
/
webService.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
// web
var http = require('http'),
express = require('express'),
path = require('path');
// database
var nedb = require('nedb'),
dbPath = 'C:\\nodejs\\filestore.nedb',
db = new nedb({ filename: dbPath, autoload: true });
// express
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'public')));
// other
var isNumeric = require('isnumeric');
var htmlBody = '';
var htmlHeader = '<!DOCTYPE html><html><body><form action="/find" method="get"><input type="text" name="nummer"></input><input type="submit" value="suchen"></input></form><br /><table>';
var htmlEnd = '</table></body></html>'
app.get('/', function (req, res) {
res.send('<html><body><form action="/find" method="get"><input type="text" name="nummer"></input><input type="submit" value="suchen"></input></form></body></html>');
});
app.get('/:a?/:b?', function (req,res) {
htmlBody = '';
var param = '';
if(req.params.b == undefined && req.query['nummer'] == undefined) {
// no parameter, do nothing
} else {
if (req.params.b == undefined) { param = req.query['nummer']; };
if (req.query['nummer'] == undefined) { param = req.params.b; };
}
switch(req.params.a) {
case 'find':
if (isNumeric(param)) {
findWhat = param;
var rePattern = new RegExp(findWhat);
db.find({id: rePattern}, function (err, docs) {
// generate htmlBody
docs.forEach(function(item){
htmlBody = htmlBody + '<tr><td>' + item.path + '</td>' + '<td><a href="/get/' + item._id + '" target="_blank">Download</a>' + '</td></tr>'
})
res.send(htmlHeader + htmlBody + htmlEnd);
});
} else {
res.send('<html><body><h1>Input not numeric</h1></body></html>');
}
break;
case 'get':
db.find({_id: param}, function (err, docs) {
if (docs.length > 0) {
res.sendFile(docs[0].path);
} else {
res.send('<h1>Document not found</h1>');
}
});
break;
default:
res.send('<html><body><h1>Please use FIND (do search in DB) or GET(do retrive File)</h1></body></html>');
}
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});