@@ -34,6 +34,8 @@ http.createServer(function (req, res) {
3434
3535` request ` is the node HTTP request. ` types ` is an array of types.
3636
37+ <!-- eslint-disable no-undef -->
38+
3739``` js
3840// req.headers.content-type = 'application/json'
3941
@@ -54,6 +56,8 @@ Having a body has no relation to how large the body is (it may be 0 bytes).
5456This is similar to how file existence works. If a body does exist, then this
5557indicates that there is data to read from the Node.js request stream.
5658
59+ <!-- eslint-disable no-undef -->
60+
5761``` js
5862if (typeis .hasBody (req)) {
5963 // read the body, since there is one
@@ -68,6 +72,8 @@ if (typeis.hasBody(req)) {
6872
6973` mediaType ` is the [ media type] ( https://tools.ietf.org/html/rfc6838 ) string. ` types ` is an array of types.
7074
75+ <!-- eslint-disable no-undef -->
76+
7177``` js
7278var mediaType = ' application/json'
7379
@@ -92,12 +98,15 @@ typeis.is(mediaType, ['html']) // false
9298
9399## Examples
94100
95- #### Example body parser
101+ ### Example body parser
96102
97103``` js
98- var typeis = require (' type-is' );
104+ var express = require (' express' )
105+ var typeis = require (' type-is' )
106+
107+ var app = express ()
99108
100- function bodyParser (req , res , next ) {
109+ app . use ( function bodyParser (req , res , next ) {
101110 if (! typeis .hasBody (req)) {
102111 return next ()
103112 }
@@ -106,22 +115,19 @@ function bodyParser(req, res, next) {
106115 case ' urlencoded' :
107116 // parse urlencoded body
108117 throw new Error (' implement urlencoded body parsing' )
109- break
110118 case ' json' :
111119 // parse json body
112120 throw new Error (' implement json body parsing' )
113- break
114121 case ' multipart' :
115122 // parse multipart body
116123 throw new Error (' implement multipart body parsing' )
117- break
118124 default :
119125 // 415 error code
120126 res .statusCode = 415
121127 res .end ()
122- return
128+ break
123129 }
124- }
130+ })
125131```
126132
127133## License
0 commit comments