-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
36 lines (31 loc) · 955 Bytes
/
index.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
var uglify = require('uglify-js')
, clientCode = require('fs').readFileSync(__dirname + '/client.js').toString()
, jsClient = uglify.minify(clientCode).code
, winston
;
function getClient(req, res) {
res.header('Content-Type', 'application/javascript');
res.end(jsClient);
}
function logMessage(req, res) {
let meta = parseMetadataSafely(req.params.meta);
winston.log(req.params.level, req.params.message, meta);
res.json({got: 'it'});
}
function parseMetadataSafely(meta) {
let parsedMeta = '';
if(!meta) return parsedMeta;
try {
parsedMeta = JSON.parse(meta);
} catch(error) {
parsedMeta = meta.toString();
}
return parsedMeta;
}
function helpExpress(app, _winston) {
winston = _winston;
jsClient = jsClient.replace('{LEVELS:LEVELS}', JSON.stringify(winston.levels));
app.get('/winston/client.js', getClient);
app.get('/winston/log/:level/:message/:meta?', logMessage);
}
module.exports = helpExpress;