-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyApp.js
120 lines (65 loc) · 1.86 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
require('dotenv').config()
// Logger
app.use((req, res, next) => {
let loggerString = req.method + " " + req.path + " - " + req.ip;
console.log('Time:', Date.now())
console.log(loggerString);
next()
})
// Middleware to server static files from public folder
app.use("/public", express.static(__dirname + '/public'));
// body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Sending Response to Get Method
// app.get('/', (req, res) => {
// res.send('Hello Express');
// })
// index route response using a static file
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
})
// chained middlewares
app.get('/now', (req, res, next) => {
req.time = new Date().toString();
console.log(req.time);
next();
}, (req, res) => {
res.json({
time : req.time,
});
});
// echo word Get Route Parameter Input from the Client
app.get('/:word/echo', (req, res, next) => {
let word = req.params.word;
res.json({
echo : word,
})
})
// name API Get Query Parameter Input from the Client
app.get('/name', (req, res, next) => {
let { first: firstname, last: lastname } = req.query;
res.json({
name : `${firstname} ${lastname}`,
})
})
app.post('/name', (req, res, next) => {
let { first: firstname, last: lastname } = req.body;
res.json({
name : `${firstname} ${lastname}`,
})
})
// Hello node console
console.log("Hello World");
// JSON API
app.get('/json', (req, res) => {
let messageObj = { message : "Hello json" }
if (process.env.MESSAGE_STYLE === 'uppercase') {
messageObj.message = messageObj.message.toUpperCase();
}
res.json(messageObj);
})
module.exports = app;