-
Notifications
You must be signed in to change notification settings - Fork 0
/
myApp.js
104 lines (53 loc) · 1.53 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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
require('dotenv').config();
//Mount the express.static() middleware to the path /public with app.use().
app.use( "/public/", express.static( __dirname + "/public/"));
app.use( bodyParser.urlencoded({extended: false}) );
app.use( "/", function(req, res, next){
console.log(req.method + " " + req.path + " - " + req.ip);
next();
});
app.get( "/now", function(req, res, next){
req.time = new Date().toString();
next();
},
function(req, res) {
res.send({"time": req.time});
}
);
app.get( "/:word/echo", (req, res) => {
res.json({
echo: req.params.word
});
});
app.route( "/name" )
.get( (req, res) => {
res.json({
name: req.query.first + ' ' + req.query.last
});
})
.post( (req, res) => {
res.json({
name: req.body.first + ' ' + req.body.last
});
});
app.get( "/json", (req, res) => {
const mySecret = process.env['MESSAGE_STYLE'];
var response = "Hello json";
console.log(mySecret)
if(mySecret === "uppercase"){
response = "Hello json".toUpperCase();
}
res.json({
message: response
});
});
//Use the app.get() method to serve all GET requests matching the / (root) path.
app.get( "/", function (req, res) {
//res.send('Hello Express') //Send 'Hello Express'
//Send a html file
res.sendFile ( __dirname + "/views/index.html");
});
module.exports = app;