-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (42 loc) · 1.07 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
var restify = require('restify');
var faker = require('faker');
var server = restify.createServer({
name: 'InfoSearch',
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
function getRandomTitle(type) {
switch (type) {
case 'company':
return faker.company.companyName();
case 'person':
return faker.name.findName();
case 'address':
return [
faker.address.streetAddress('##'),
faker.address.city(),
faker.address.state(),
faker.address.zipCode('####')
].join(', ');
}
}
server.get('/api/search', function (req, res, next) {
const results = [];
const term = req.params.term;
const type = ['company', 'person', 'address'][Math.floor(Math.random() * 3)];
for (var i = 0; i < 5; i++) {
results.push({
title: getRandomTitle(type) + ' ' + term
});
}
res.send({
type: type,
results: results
});
return next();
});
server.listen(3000, function () {
console.log('%s listening at %s', server.name, server.url);
});