forked from freeCodeCamp/boilerplate-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyApp.js
86 lines (42 loc) · 1.07 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
let express = require('express');
let app = express();
let bodyParser = require("body-parser");
app.use("/public", express.static(__dirname + "/public"));
// #11
app.use(bodyParser.urlencoded({extended: false}));
// #7
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
});
//
app.get("/", function(req, res) {
res.sendFile(__dirname + "/views/index.html")
});
app.get("/json", (req, res) => {
if (process.env["MESSAGE_STYLE"] === "uppercase") {
res.json({"message":"HELLO JSON"});
} else {
res.json({"message":"Hello json"});
}
});
// #8
app.get("/now", (req, res, next) => {
req.time = new Date().toString();
next();
}, (req, res) => {
res.json({"time": req.time});
});
// #9
app.get("/:word/echo", (req, res) => {
res.json({"echo": req.params.word});
});
// #10
app.get("/name", (req, res) => {
res.json({name: req.query.first + " " + req.query.last});
});
//#12
app.post("/name", (req, res) => {
res.json({name: req.body.first + " " + req.body.last});
});
module.exports = app;