This repository was archived by the owner on Oct 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbyo.coffee
More file actions
65 lines (54 loc) · 1.44 KB
/
byo.coffee
File metadata and controls
65 lines (54 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!
# * Connect - BYO Body Parser
# as in "Bring your own"
# * Copyright(c) 2010 Sencha Inc.
# * Copyright(c) 2011 TJ Holowaychuk
# * MIT Licensed
#
# * Copyright(c) 2013 Chris Continanza
###
Module dependencies.
###
###
noop middleware.
###
noop = (req, res, next) ->
next()
utils = require("express/node_modules/connect/lib/utils")
_limit = require("express/node_modules/connect/lib/middleware/limit")
###
JSON:
Parse logplex request bodies, providing the
parsed object as `req.body`.
Options: none
@param content_type {String} use when Content-Type matches this string
@param parser {Function} parsing function takes String body and returns new body
@return {Function}
@api public
###
exports = module.exports = (options = {}) ->
limit = (if options.limit then _limit(options.limit) else noop)
logplex = (req, res, next) ->
return next() if req._body
req.body = req.body or {}
return next() unless utils.hasBody(req)
# check Content-Type
return next() unless options.content_type is utils.mime(req)
# flag as parsed
req._body = true
# parse
limit req, res, (err) ->
return next(err) if err
buf = ""
req.setEncoding "utf8"
req.on "data", (chunk) ->
buf += chunk
req.on "end", ->
first = buf.trim()
try
req.body = options.parser(buf)
catch err
err.body = buf
err.status = 400
return next(err)
next()