-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
106 lines (94 loc) · 2.23 KB
/
app.js
File metadata and controls
106 lines (94 loc) · 2.23 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use('/assets', express.static('assets'));
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/doubling', require('./doubling-controller'));
app.get('/greeter', function (req, res) {
if (req.query.name === undefined) {
res.body = {
"error": "Please provide a name!"
}
} else if (req.query.title === undefined) {
res.body = {
"error": "Please provide a title!"
}
} else {
res.body = {
"welcome_message": "Oh, hi there " + req.query.name + ", my dear " + req.query.title + "!"
}
}
res.json(res.body);
});
app.get('/appenda/:word', function (req, res) {
if (req.params.word != null) {
res.body = {
"appended": req.params.word + "a"
}
res.json(res.body);
} else {
res.send(404);
}
});
app.post('/dountil/:what', function (req, res) {
let body;
if (req.params.what != null) {
if (req.params.what === 'sum') {
let sum = 0;
for (let i = 1; i <= req.body.until; i++ ) {
sum += i;
}
body = {
"result": sum
}
} else if (req.params.what === 'factor') {
let factor = 1;
for (let i = 1; i <= req.body.until; i++ ) {
factor *= i;
}
body = {
"result": factor
}
}
} else {
body = {
"error": "Please provide a number!"
}
}
res.json(body);
});
app.post('/arrays', function (req, res) {
let body;
console.log(req.body);
if (req.body.what === 'sum') {
let sum = 0;
for (let i = 0; i < req.body.numbers.length; i++ ) {
sum += req.body.numbers[i];
}
body = {
"result": sum
}
} else if (req.body.what === 'multiply') {
let multi = 1;
for (let i = 0; i < req.body.numbers.length; i++ ) {
multi *= req.body.numbers[i];
}
body = {
"result": multi
}
} else if (req.body.what === 'double') {
body = {
"result": req.body.numbers.map(item => item * 2)
}
} else {
body = {
"error": "Please provide what to do with the numbers!"
}
}
res.json(body);
});
module.exports = app