forked from alsotang/node-lessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (34 loc) · 707 Bytes
/
app.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
var express = require('express');
var fibonacci = function (n) {
if (typeof n !== 'number' || isNaN(n)) {
throw new Error('n should be a Number');
}
if (n < 0) {
throw new Error('n should >= 0')
}
if (n > 10) {
throw new Error('n should <= 10');
}
if (n === 0) {
return 0;
}
if (n === 1) {
return 1;
}
return fibonacci(n-1) + fibonacci(n-2);
};
var app = express();
app.get('/fib', function (req, res) {
var n = Number(req.query.n);
try {
res.send(String(fibonacci(n)));
} catch (e) {
res
.status(500)
.send(e.message);
}
});
module.exports = app;
app.listen(3000, function () {
console.log('app is listening at port 3000');
});