-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
68 lines (54 loc) · 1.53 KB
/
app.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
var qs = require('querystring');
var url = require('url');
var dataUtils = require('./dataUtils');
var args = process.argv.slice(2);
var port = args[0] || 3000;
//throw an error? nah - just ignore
if(isNaN(port)) port=3000;
require('http').createServer(function(req, res) {
var dataReq = qs.parse(url.parse(req.url).query);
//console.log("dataReq",dataReq);
//Did they specify how many they want?
if(!dataReq.num) dataReq.num = 10;
if(typeof dataReq.num == "string" && dataReq.num.indexOf(":") > 0) {
var parts = dataReq.num.split(":");
if(parts[0] != "rnd") {
throw new Error("Invalid num prefix sent. Must be 'rnd'");
}
// format is rnd:10 which means, from 1 to 10
if(parts.length === 2) {
dataReq.num = dataUtils.randRange(1,parts[1]);
} else {
dataReq.num = dataUtils.randRange(parts[1],parts[2]);
}
}
/* for my testing...
var fieldModel = [
{name:"name"},
{name:"fname"},
{name:"lname"},
{name:"age"},
{name:"all_age"},
{name:"email"},
{name:"ssn"},
{name:"tel"},
{name:"goo",type:"lname"},
{name:"num"},
{name:"upto20",type:"num:20"},
{name:"20to30",type:"num:20:30"}
];
*/
var fieldModel = [];
for(key in dataReq) {
if(key != "num") {
fieldModel.push({name:key,type:dataReq[key]});
}
}
var result = [];
for(var i=0; i<dataReq.num; i++) {
result.push(dataUtils.generateNewItem(fieldModel));
}
res.writeHead(200, {'Content-Type':'application/json','Access-Control-Allow-Origin':'*'});
res.end(JSON.stringify(result));
}).listen(port);
console.log("Listening on "+port);