-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
82 lines (74 loc) · 2.11 KB
/
server.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// simple faker json api server
// handles all faker APIs
// returns json payload with key of "result" or "error"
const help = [
'Functions can take optional arg as value or object:',
'All Faker.js functions are callable',
'URI fake general form: http://<server>:<port>/faker/<group>/<function>',
'URI should be encoded with javascript:encodeURI() or php:rawurlencode()',
'Note: path /faker/random/number corresponds to function faker.random.number',
'Example paths:',
'/faker/random/number',
'/faker/random/number?42',
'/faker/random/number?{"max":42}',
'/faker/random/float?{"max":1.99,"min":1,"precision":0.0001}',
'/faker/name/findName',
'A complex fake can be specified using the mustache form:',
'/faker/fake({{name.lastName}}, {{name.firstName}})',
'Pass array in query string by wrapping values with parens:',
'/faker/helpers/randomize?("one","two","three")',
];
const jsonServer = require('json-server');
const server = jsonServer.create();
const middlewares = jsonServer.defaults();
const port = process.env.PORT || 3000;
const faker = require('faker');
server.use(middlewares);
/**
* Service a faker request
*/
server.get('/faker/*', (req, res) => {
const path = req.path
const query = req.query
const tag = path.split('/').pop();
const fn = path.split('/').slice(2).reduce((acc, cur) => {
return acc[cur]
}, faker);
const key = Object.keys(query)[0]
const parseOpts = args => {
try {
return JSON.parse(args);
} catch (e) {
return args
}
}
// options as array in form of (arg, arg ...)
let opts
if (typeof key === 'string' && key.match(/^\(.*\)$/)) {
opts = key.match(/^\((.*)\)$/)[1].split(",").map(parseOpts)
} else {
opts = typeof key === 'string' ? parseOpts(key) : key
}
let result
try {
result = fn(opts)
} catch (e) {
result = 'exception: ' + e.message
}
res.status(200).jsonp({
path,
tag,
opts,
result,
});
});
/**
* Help
*/
server.get('/help', (req, res) => {
res.status(200).jsonp({
help,
});
});
server.listen(port);
console.log(`faker api server running on http://localhost:${port}`);