@@ -226,23 +226,23 @@ module.exports = function (request, response) {
226
226
` POST.js ` file for non ExpressJS server:
227
227
228
228
``` js
229
- var fs = require (' fs' );
230
- var path = require (' path' );
229
+ const fs = require (' fs' );
230
+ const path = require (' path' );
231
231
232
- module .exports = function (request , response ) {
232
+ module .exports = (request , response ) => {
233
233
if (! request .get (' X-Auth-Key' )) {
234
234
response .statusCode = 403 ;
235
235
response .end ();
236
236
} else {
237
- var filePath = path .join (__dirname , ' POST.json' );
238
- var stat = fs .statSync (filePath);
237
+ const filePath = path .join (__dirname , ' POST.json' );
238
+ const stat = fs .statSync (filePath);
239
239
240
240
response .writeHead (200 , {
241
241
' Content-Type' : ' application/json' ,
242
242
' Content-Length' : stat .size
243
243
});
244
244
245
- var readStream = fs .createReadStream (filePath);
245
+ const readStream = fs .createReadStream (filePath);
246
246
// We replaced all the event handlers with a simple call to readStream.pipe()
247
247
readStream .pipe (response);
248
248
}
@@ -260,7 +260,7 @@ const fs = require('fs');
260
260
const path = require (' path' );
261
261
262
262
module .exports = function (request , response ) {
263
- var targetFileName = ' GET.json' ;
263
+ let targetFileName = ' GET.json' ;
264
264
265
265
// Check is a type parameter exist
266
266
if (request .query .type ) {
@@ -281,13 +281,13 @@ module.exports = function (request, response) {
281
281
```
282
282
` GET.js ` file for non ExpressJS server:
283
283
``` js
284
- var url = require (' url' );
285
- var fs = require (' fs' );
286
- var path = require (' path' );
284
+ const url = require (' url' );
285
+ const fs = require (' fs' );
286
+ const path = require (' path' );
287
287
288
288
module .exports = function (request , response ) {
289
- var targetFileName = ' GET.json' ;
290
- var typeQueryParam = url .parse (request .url , true ).query .type ;
289
+ let targetFileName = ' GET.json' ;
290
+ const typeQueryParam = url .parse (request .url , true ).query .type ;
291
291
// Check is a type parameter exist
292
292
if (typeQueryParam) {
293
293
// Generate a new targetfilename with that type parameter
@@ -306,18 +306,46 @@ module.exports = function (request, response) {
306
306
return ;
307
307
}
308
308
309
- var stat = fs .statSync (filePath);
309
+ const stat = fs .statSync (filePath);
310
310
response .writeHead (200 , {
311
311
' Content-Type' : ' application/json' ,
312
312
' Content-Length' : stat .size
313
313
});
314
314
315
- var readStream = fs .createReadStream (filePath);
315
+ const readStream = fs .createReadStream (filePath);
316
316
// We replaced all the event handlers with a simple call to readStream.pipe()
317
317
readStream .pipe (response);
318
318
}
319
319
```
320
320
321
+ ## Helper functions for custom responses
322
+
323
+ Connect-Api-Mocker also presents a bunch of helper functions to speed up writing simple custom responses. There are:
324
+
325
+ - ` status(statusCode) ` : Set status code of response
326
+ - ` notFound(message?) ` : Set status code as 404 and optionally sends message
327
+ - ` created() ` : Sets status code as 201
328
+ - ` success() ` : Sets status code as 200
329
+ - ` delay(duration) ` : Delays the request by given duration(in ms).
330
+ - ` json(data) ` : Send given JSON object as response.
331
+ - ` end(body) ` : Ends request and optionally sends the string output
332
+
333
+ You can use these functions in custom responses, like:
334
+
335
+ ``` js
336
+ const { notFound } = require (' connect-api-mocker/helpers' );
337
+
338
+ module .exports = notFound (' Page is not found' );
339
+ ```
340
+
341
+ Also you can combine multiple functions:
342
+
343
+ ``` js
344
+ const { delay , created , json } = require (' connect-api-mocker/helpers' );
345
+
346
+ module .exports = [delay (500 ), created (), json ({success: true })];
347
+ ```
348
+
321
349
## Wildcards in requests
322
350
323
351
You can use wildcards for paths to handle multiple urls(like for IDs). If you create a folder structure like ` api/users/__user_id__/GET.js ` , all requests like ` /api/users/321 ` or ` /api/users/1 ` will be responded by custom middleware that defined in your ` GET.js ` . Also id part of the path will be passed as a request parameter named as ` user_id ` to your middleware. So you can write a middleware like that:
0 commit comments