Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handler #101

Open
yookel opened this issue Aug 27, 2014 · 0 comments
Open

Error handler #101

yookel opened this issue Aug 27, 2014 · 0 comments

Comments

@yookel
Copy link

yookel commented Aug 27, 2014

I built an error handler...

// server code....

var express = require('express.io');
var app = express().http().io();

function middleware_errorhandler(err, req, res, next) {
var env = 'development';
switch (env) {
case 'development':
console.log('Err:',err);
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
var errmsg = err.message+'\n';
errmsg += err.status+'\n';
errmsg += err.stack+'\n';
res.end(errmsg);
break;
default:
res.end('Server error');
};
}

var clienthtml = ' \

\ \ \ \ <script src="//code.jquery.com/jquery-1.10.2.js"></script> \ <script src="/socket.io/socket.io.js"></script> \ <script> \ var socket = io.connect(); \ function onc(wheretogo) { \ $("#ioon").html("Clicked"); \ if (wheretogo) { socket.emit("whatserr") } \ else { socket.emit("whatsoke") } \ }; \ socket.on("yooo", function(data) { $("#ioon").html(data.yoholo) }); \ <\/script> \ <\/head> \ \

Yo world!<\/h1> \ Emit-me-err<\/button> \ Emit-me-oke<\/button> \

io-on<\/p> \ <\/body> \ <\/html> \ ';

function body(req, res, next) {
res.setHeader('Content-Type', 'text/html');
res.end(clienthtml);
}

var ioroute = function (rte,iox) {
app.io.route(rte, function(req) {
try {
console.log('ioroute is trying...');
iox(req);
}
catch(err) {
console.log('Oopzzz an err:',err);
}
});
};

ioroute('whatsoke',function(req) {
console.log('Whatzzzokeeee...');
req.io.emit('yooo', {yoholo:"Yoho!"}) ;
});

ioroute('whatserr',function(req) {
console.log('Whatzzzerrrrrrr...');
foo();
req.io.emit('yooo', {yoholo:"Yoho!"}) ;
});

app.use(body); // the body ...
app.use(middleware_errorhandler);

app.listen(5000);
console.log('Server started @ 5000');

bpytlik pushed a commit to bpytlik/express.io that referenced this issue Aug 28, 2014
bpytlik pushed a commit to bpytlik/express.io that referenced this issue Aug 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant