-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (67 loc) · 2.4 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
var express = require('express');
var fs = require('fs');
var path = require('path');
var util = require('util');
var Protector = require('./Protector');
var fell = require('fell');
fell.Log.configure("debug");
var log = fell.Log.getLogger('server');
var port = 3000;
var cssProtector = new Protector();
var app = express();
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
};
app.use(allowCrossDomain);
app.post('/data', function(req, res){
var scanResult = req.body;
log.info("Received scan result from client : {0}", JSON.stringify(scanResult));
cssProtector.submitScan(scanResult, function(err, doc) {
if (err) {
log.error("There was a problem adding the information to the database: {0}.", err);
res.send("There was a problem adding the information to the database.");
} else {
var body = JSON.stringify(scanResult);
log.debug("Scan result added to the database. {0}.", body);
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', Buffer.byteLength(body));
res.end(body);
}
});
});
app.get('/', function(req, res){
log.debug('Got request for index.');
cssProtector.getAllScans(function(err, docs) {
if (err) {
log.error('Error reading from the database : {0}', err);
}
res.render('index', {title: 'cssProtector', results: docs});
});
});
app.get('/diff', function(req, res) {
log.debug('Got request for diff.');
var firstTime = Number(req.query.first);
var secondTime = Number(req.query.second);
cssProtector.diff(firstTime, secondTime, function(error, differences) {
if (error) {
log.error("Problem diffing {0} and {1} : {2}", firstTime, secondTime, error);
}
if (differences) {
log.debug('Differences = {0}', util.inspect(differences,{depth:10}));
res.render('diff', {title: 'cssProtector', differences: differences});
} else {
log.debug('No differences between {0} and {1}.', firstTime, secondTime);
res.render('nodifference', {first: firstTime, second: secondTime});
}
});
});
app.listen(port);
log.info('Listening on port {0}.', port);