-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjamfAnimals.js
More file actions
executable file
·51 lines (39 loc) · 1.41 KB
/
jamfAnimals.js
File metadata and controls
executable file
·51 lines (39 loc) · 1.41 KB
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
const http = require('http');
const url = require('url');
const dog = {animal:'dog', details:{genus:'Canis', sound:'Bark', habitat:'Dog House', food:'Kibbles & Bits'}};
const lion = {animal:'lion', details:{genus:'Panthera', sound:'Roar', habitat:'Savannah', food:'Antelope'}};
const horse = {animal:'horse', details:{genus:'Equus', sound:'Neigh', habitat:'Farm', food:'Hay'}};
const animals = {'dog':dog, 'lion':lion, 'horse':horse};
var port = parseInt(process.argv[2]);
var server = http.createServer(function(req, res){
var reqURLData = url.parse(req.url, true);
var pathname = reqURLData.pathname;
var query = reqURLData.query;
var method = req.method;
if(method != 'GET'){
sendError(res, 'Invalid Request');
}
if(pathname == '/api/details') {
var animalString = query.animal;
console.log('request received for '+animalString);
var animal = animals[animalString];
if(animal){
sendAnimalDetails(res, animal);
} else {
sendError(res, 'Animal not supported');
}
} else {
sendError(res, 'Invalid Request');
}
res.end();
});
server.listen(port);
function sendError(response, errMessage){
var error = {error:errMessage};
response.writeHead(404, {'Content-Type':'application/json'});
response.write(JSON.stringify(error));
}
function sendAnimalDetails(response, animal){
response.writeHead(200, {'Content-Type':'application/json'});
response.write(JSON.stringify(animal));
}