From 7518b688d69fc80cd8f6e94e43cb3ab086261bbf Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Thu, 19 Nov 2015 18:06:37 +0100 Subject: [PATCH] basic 404 handler, #13 --- src/server.coffee | 11 ++++++++++- views/404.jade | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 views/404.jade diff --git a/src/server.coffee b/src/server.coffee index b2637cb..04a056c 100644 --- a/src/server.coffee +++ b/src/server.coffee @@ -32,11 +32,19 @@ errorHandler = (err, req, res, next) -> log.error "ERROR", StringifySafe err delete err.arguments res.status 400 - if Accepts(req).types().length > 0 and Accepts(req).types()[0] is 'text/html' + if Accepts(req).type('text/html') return res.render 'error', { err } else res.send StringifySafe err, null, 2 +notFoundHandler = (req, res) -> + if Accepts(req).type('text/html') + return res.render '404', {reqJSON: StringifySafe(req, null, 2)} + else + res.end() + + + accessLogger = Morgan 'combined', stream: Fs.createWriteStream(__dirname + '/../data/logs/access.log', {flags: 'a'}) accessLoggerDev = Morgan 'dev' @@ -122,6 +130,7 @@ class InfolisWebservice res.end() # Error handler @app.use errorHandler + @app.use notFoundHandler startServer : () -> log.info "Setting up routes" diff --git a/views/404.jade b/views/404.jade new file mode 100644 index 0000000..decc139 --- /dev/null +++ b/views/404.jade @@ -0,0 +1,12 @@ +extends ./layout.jade + +block title + | 404 Not Found + +block content + - req = JSON.parse(reqJSON) + .row + .col-md-offset-1 + h1 404 Not Found + p There is no such thing + pre= JSON.stringify(req.headers, null, 2)