Skip to content
Permalink
Browse files
[#57] Add console.log support
  • Loading branch information
purplecabbage authored and timkim committed Jul 23, 2014
1 parent 43cae5b commit e8dff0927a3bf98ee3440a9bac4e0e6d1aeb4ce7
Showing with 45 additions and 5 deletions.
  1. +2 −0 lib/middleware/inject.js
  2. +20 −1 lib/serve.js
  3. +4 −4 package.json
  4. +19 −0 res/middleware/consoler.js
@@ -12,13 +12,15 @@ var fs = require('fs'),

module.exports = function() {
var autoreloadScript = path.join(__dirname, '../../res/middleware/autoreload.js'),
consoleScript = path.join(__dirname, '../../res/middleware/consoler.js'),
homepageScript = path.join(__dirname, '../../res/middleware/homepage.js'),
proxyScript = path.join(__dirname, '../../res/middleware/proxy.js'),
refreshScript = path.join(__dirname, '../../res/middleware/refresh.js');

return inject({
snippet: [
fs.readFileSync(autoreloadScript),
fs.readFileSync(consoleScript),
fs.readFileSync(homepageScript),
fs.readFileSync(proxyScript),
fs.readFileSync(refreshScript)
@@ -57,7 +57,8 @@ module.exports = function(options) {

// create the server
var pg = middleware(options),
server = http.createServer(pg);
server = http.createServer(pg),
io = require('socket.io')(server);

// bind error
server.on('error', function(e) {
@@ -106,6 +107,24 @@ module.exports = function(options) {
server.emit.apply(server, args);
});

io.on('connection', function (socket) {
socket.on('console', function (type,data) {
switch(type) {
case 'warn' :
server.emit('log', '[console.warn]'.yellow, data);
break;
case 'error' :
server.emit('log', '[console.error]'.red, data);
break;
case 'log' : // intentional fallthrough
default :
server.emit('log', '[console.log]'.green, data);
break;
}

});
});

// start the server
return server.listen(options.port);
};
@@ -24,22 +24,22 @@
"dependencies": {
"connect": "2.12.0",
"connect-inject": "0.3.2",
"gaze" : "0.4.3",
"gaze": "0.4.3",
"home-dir": "0.1.2",
"node-static": "0.7.0",
"request": "2.33.0",
"request-progress": "0.3.1",
"shelljs": "0.2.6",
"tar": "0.1.19",
"useragent": "2.0.8"
"useragent": "2.0.8",
"socket.io": "~1.0.4"
},
"devDependencies": {
"chdir": "0.0.0",
"jasmine-node": "1.8.0",
"supertest": "0.13.0"
},
"optionalDependencies": {
},
"optionalDependencies": {},
"contributors": [
{
"name": "Michael Brooks",
@@ -0,0 +1,19 @@
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://' + document.location.host);
var previousConsole = window.console || {};
window.console = {
log:function(msg){
previousConsole.log && previousConsole.log(msg);
socket.emit('console','log', msg);
},
warn:function(msg){
previousConsole.warn && previousConsole.warn(msg);
socket.emit('console','warn', msg);
},
error:function(msg){
previousConsole.error && previousConsole.error(msg);
socket.emit('console','error', msg);
}
}
</script>

0 comments on commit e8dff09

Please sign in to comment.