-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (33 loc) · 1.13 KB
/
index.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
/*
* Primary file for the API
* This is a bare-bones API that returns a quote by Walt Disney when called.
*/
// Dependencies
var http = require('http');
var quotesLib = require('./lib/quotes');
// App object
var app = {};
// Instantiate the HTTP server
var httpServer = http.createServer(function(req,res) {
var allQuotes = quotesLib.allQuotes();
var numberOfJokes = allQuotes.length;
var randomNumber = app.getRandomNumber(1,numberOfJokes);
var selectedQuote = allQuotes[randomNumber - 1];
// Conert the payload to a string
var payload = { 'quote' : selectedQuote, 'attributedTo' : 'Walt Disney' };
var payloadString = JSON.stringify(payload);
// Return the response
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end(payloadString);
});
// Start the HTTP server
httpServer.listen(3000, function() {
console.log("The server is listening on port 3000");
});
// Get a random number
app.getRandomNumber = function(min,max){
min = typeof(min) == 'number' && min % 1 === 0 ? min : 0;
max = typeof(max) == 'number' && max % 1 === 0 ? max : 0;
return Math.floor(Math.random()*(max-min+1)+min);
};