From b802119f2b517c4057462717cd0ebd4bbf822040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Aparicio?= Date: Fri, 30 Aug 2013 12:58:22 +0200 Subject: [PATCH] #2 Fix HTTP module error while trying to write response headers --- src/portals/admin.coffee | 15 ++++++++------- src/portals/portal.coffee | 4 ++++ src/portals/stubs.coffee | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/portals/admin.coffee b/src/portals/admin.coffee index 4524214..17813d3 100644 --- a/src/portals/admin.coffee +++ b/src/portals/admin.coffee @@ -6,6 +6,7 @@ path = require 'path' status = new ns.Server (path.resolve __dirname, '../../webroot') module.exports.Admin = class Admin extends Portal + constructor : (endpoints) -> @endpoints = endpoints @contract = contract @@ -14,7 +15,7 @@ module.exports.Admin = class Admin extends Portal urlPattern : /^\/([1-9][0-9]*)?$/ goPong: (response) -> - response.writeHead 200, {'Content-Type' : 'text/plain'} + @writeHead response, 200, {'Content-Type' : 'text/plain'} response.end 'pong' goPUT : (request, response) -> @@ -92,14 +93,14 @@ module.exports.Admin = class Admin extends Portal @endpoints.create data, callback ok : (response, result) -> - response.writeHead 200, {'Content-Type' : 'application/json'} + @writeHead response, 200, {'Content-Type' : 'application/json'} if result? response.end(JSON.stringify result) else response.end() created : (response, request, id) -> - response.writeHead 201, {'Location' : "#{request.headers.host}/#{id}"} + @writeHead response, 201, {'Location' : "#{request.headers.host}/#{id}"} response.end() noContent : (response) -> @@ -107,7 +108,7 @@ module.exports.Admin = class Admin extends Portal response.end() badRequest : (response, errors) -> - response.writeHead 400, {'Content-Type' : 'application/json'} + @writeHead response, 400, {'Content-Type' : 'application/json'} response.end JSON.stringify errors notSupported : (response) -> @@ -115,15 +116,15 @@ module.exports.Admin = class Admin extends Portal response.end() notFound : (response) -> - response.writeHead 404, {'Content-Type' : 'text/plain'} + @writeHead response, 404, {'Content-Type' : 'text/plain'} response.end() saveError : (response) -> - response.writeHead 422, {'Content-Type' : 'text/plain'} + @writeHead response, 422, {'Content-Type' : 'text/plain'} response.end() serverError : (response) -> - response.writeHead 500, {'Content-Type' : 'text/plain'} + @writeHead response, 500, {'Content-Type' : 'text/plain'} response.end() urlValid : (url) -> diff --git a/src/portals/portal.coffee b/src/portals/portal.coffee index d847eb2..9dee19a 100644 --- a/src/portals/portal.coffee +++ b/src/portals/portal.coffee @@ -6,6 +6,10 @@ module.exports.Portal = class Portal constructor: -> @name = 'portal' + writeHead: (response, status_code, headers) -> + response.writeHead status_code, headers if !response.headersSent + return response + received: (request, response) -> date = new Date() hours = "0#{date.getHours()}".slice -2 diff --git a/src/portals/stubs.coffee b/src/portals/stubs.coffee index f1d081d..889c105 100644 --- a/src/portals/stubs.coffee +++ b/src/portals/stubs.coffee @@ -23,10 +23,10 @@ module.exports.Stubs = class Stubs extends Portal callback = (err, endpointResponse) => if err - response.writeHead 404, {} + @writeHead response, 404, {} @responded 404, request.url, 'is not a registered endpoint' else - response.writeHead endpointResponse.status, endpointResponse.headers + @writeHead response, endpointResponse.status, endpointResponse.headers response.write endpointResponse.body @responded endpointResponse.status, request.url