-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrandomuser.js
57 lines (51 loc) · 1.36 KB
/
randomuser.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
var http = require('http');
var faker = require('faker');
var port = process.env.PORT || 3000;
var ipAddress = process.env.IP_ADDRESS || '127.0.0.1';
http.createServer(function(req, res) {
wait(500);
sendJsonResponse(res, getRandomUserApiUser());
}).listen(port, ipAddress);
console.log('Server running at ' + ipAddress + ':' + port);
function wait(ms) {
var start = new Date();
while (new Date() - start < ms) {
}
}
function getRandomUserApiUser() {
return {
results: [
{user: getRandomUser()}
]
};
}
function getRandomUser() {
return {
name: {
first: faker.name.firstName(),
last: faker.name.lastName()
},
username: faker.internet.userName(),
email: faker.internet.email(),
picture: {
medium: Math.random() > .5 ? 'fred.png' : 'ethel.png'
},
cell: faker.phone.phoneNumber(),
location: {
street: faker.address.streetAddress(),
city: faker.address.city(),
state: faker.address.state(),
zip: faker.address.zipCode()
},
dob: (Math.random() * 515576000) + 94672800
};
}
function sendJsonResponse(res, data) {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
});
res.end(JSON.stringify(data));
}