sleeprest
Version:
Resource driven REST server.
47 lines (37 loc) • 990 B
text/coffeescript
## Extend for the request
http = require 'http'
Request = http.IncomingMessage
Promise = require 'bluebird'
readOnly = (obj, prop, get) ->
Object.defineProperty obj::, prop,
get: get
try
# contentType getter
readOnly Request, 'contentType', ->
type = ['content-type']
||= if not type?
# RFC2616 section 7.2.1 # Restify stolen :p
'application/octet-stream'
else
if (index = type.indexOf ';') is -1
type
else
type.substring 0, index
catch error
"Do very nothing.."
Request::loadBody = (opts) ->
{timeout} = opts ||
timeout: 2000 # Set a default timeout of 2 seconds
if ?
Promise.resolve
new Promise (yell, cry) =>
body = []
'readable', ->
data =
return unless data?
body.push data
.on 'end', =>
= Buffer.concat body
yell
.on 'error', cry
.timeout timeout