-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (43 loc) · 1.33 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
47
48
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var multer = require('multer'); // v1.0.5
var upload = multer(); // for parsing multipart/form-data
var app = express();
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public"));
app.use(express.static('static'));
// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({
"error": message
});
}
// FizzBuzz API Routes
app.post("/fizzbuzz", function (req, res, next) {
console.log(req.body);
var num = parseInt(req.body.number);
if (isNaN(num)) {
res.status(400).send("Bad Request: " + req.body);
} else {
if (num % 15 == 0) {
num = "Fizz Buzz";
} else if (num % 5 == 0) {
num = "Buzz";
} else if (num % 3 == 0) {
num = "Fizz";
}
console.log(num);
res.status(200).send(String(num));
}
next();
});
app.get("/fizzbuzz", function(req, res) {
res.status(200).send("Try send a POST request.\ne.g. {\"number\": 3}");
});