-
Notifications
You must be signed in to change notification settings - Fork 0
/
myApp.js
94 lines (49 loc) · 1.41 KB
/
myApp.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
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
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
require('dotenv').config();
console.log("Hello World");
let absolutePath = __dirname + '/views/index.html'
app.use('/public', express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }))
const logger = (req, res, next) => {
const method = req.method;
const path = req.path;
const ip = req.ip;
console.log(`${method} ${path} - ${ip}`);
next();
}
const addTimestamp = (req, res, next) => {
req.time = new Date().toString();
next();
};
app.use(logger);
app.get('/', (req, res) => {
res.sendFile(absolutePath);
});
app.get('/json', (req, res) => {
const message = "Hello json";
if (process.env.MESSAGE_STYLE === "uppercase") {
res.json({ message: message.toUpperCase() });
} else {
res.json({ message: message });
}
});
app.get('/now', addTimestamp, (req, res) => {
res.json({time: req.time});
});
app.get('/:word/echo', (req, res) => {
res.json({echo: req.params.word});
});
app.get('/name', (req, res) => {
let firstname = req.query.first;
let lastname = req.query.last;
res.json({
name: firstname + " " + lastname,
});
});
app.post('/name', (req ,res) => {
let fullName = req.body.first + ' ' + req.body.last;
res.json({name: fullName});
});
module.exports = app;