forked from freeCodeCamp/boilerplate-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyApp.js
84 lines (49 loc) · 1.47 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
require("dotenv").config();
let express = require('express');
let app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
//__dirname/public is mounted in /public dir of our website
app.use("/public", express.static(__dirname + "/public"))
//const handler = (req, res) => {
// res.send("Hello Express")
//}
//middleware for all routes
app.use(function(req, _, next) {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next()
});
//sendfile in the route response
const absolutePath = __dirname + '/views/index.html'
app.get("/", (_, res) => {
res.sendFile(absolutePath)
});
//sending jason
app.get("/json", (_, res) => {
let message = "Hello json"
res.json({ "message": `${process.env.MESSAGE_STYLE === "uppercase" ? message.toUpperCase() : message}` })
});
//chaining middleware in /now route
app.get("/now", function(req, _, next) {
req.time = new Date().toString();
next();
}, function(req, res) {
res.json({ "time": `${req.time}` })
/* eslint-disable */
/* eslint-disable */
})
app.get("/:word/echo", function(req, res) {
const { word } = req.params;
res.json({ "echo": `${word}` })
})
//route with chained methods
app.route("/name")
.get(function(req, res) {
const { first, last } = req.query
res.json({ name: `${first} ${last}` })
})
.post(function(req, res) {
const { first, last } = req.body
res.json({ name: `${first} ${last}` })
})
module.exports = app;