Scribe.js/herokuapp/app.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-12-22 22:43:57 -06:00
/* jshint -W079 */
(function() {
var scribe = require('../scribe')(),
console = process.console,
express = require('express'),
2014-12-23 05:12:03 -06:00
path = require('path'),
2014-12-22 22:43:57 -06:00
app = express();
2014-12-23 05:12:03 -06:00
console.addLogger('log', 'green');
2014-12-23 13:10:46 -06:00
console.addLogger('err', 'red');
2014-12-22 22:43:57 -06:00
2014-12-23 05:12:03 -06:00
// port
2014-12-22 22:43:57 -06:00
app.set('port', (process.env.PORT || 5000));
2014-12-23 05:12:03 -06:00
// public dir
app.use('/', express.static(path.join(__dirname, 'public')));
// scribe
app.use(scribe.express.logger());
app.use('/logs', scribe.webPanel());
// index
2014-12-22 22:43:57 -06:00
app.get('/', function(req, res) {
2014-12-23 05:12:03 -06:00
res.sendFile(path.join(__dirname, 'views', 'index.html'));
2014-12-22 22:43:57 -06:00
});
2014-12-23 13:02:38 -06:00
function _param(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
var results = regex.exec("https://r.com?" + this.body);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
2014-12-23 05:12:03 -06:00
// log
2014-12-23 13:02:38 -06:00
app.post('/', function(req, res) {
req.body = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.body += chunk;
});
req.on('end', function() {
req.param = _param;
var tag = req.param('tag');
var msg = req.param('msg');
2014-12-23 13:10:46 -06:00
if (!msg) {
return res.status(400).send('Param `msg` not defined');
2014-12-23 13:02:38 -06:00
}
try {
msg = JSON.parse(msg);
} catch (e) {
// ignore
}
2014-12-22 22:43:57 -06:00
2014-12-23 13:02:38 -06:00
// print
2014-12-23 13:12:10 -06:00
if (tag) {
2014-12-23 13:02:38 -06:00
console.log(msg);
} else {
console.tag(tag).log(msg);
}
2014-12-23 05:12:03 -06:00
2014-12-23 13:10:46 -06:00
res.status(200).send("Server logged message");
2014-12-23 13:02:38 -06:00
});
2014-12-22 22:43:57 -06:00
2014-12-23 13:02:38 -06:00
req.on('close', function(err){
2014-12-23 13:10:46 -06:00
console.err(err);
res.status(400).json("Server read stream closed unexpectedly. Check console.");
2014-12-23 13:02:38 -06:00
});
2014-12-22 22:43:57 -06:00
});
2014-12-23 05:12:03 -06:00
var port = app.get('port');
2014-12-22 22:43:57 -06:00
app.listen(port, function() {
console.time().log('Server listening at port ' + port);
});
})();