forked from freeCodeCamp/boilerplate-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyApp.js
87 lines (60 loc) · 1.67 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
let express = require('express');
let app = express();
var bodyParser = require('body-parser');
var env = require('dotenv').config();
// Only use each endpoint once, if not express will use the first occurence
app.get("/", function(req, res) {
res.sendFile(__dirname + "/views/index.html");
});
// Normal usage
//app.use(express.static(__dirname + "/public"));
// Assets at the /public route
app.use("/public", express.static(__dirname + "/public"));
/*
app.get("/json", (req, res) => {
res.json({
message: "Hello json"
});
} );
*/
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.use("/json", function middleware(req, res, next) {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
})
var message= 'Hello json';
app.get("/json", (req, res) => {
if ( process.env['MESSAGE_STYLE'] === "uppercase") {
res.json({ "message": message.toUpperCase() });
}
else {
res.json({ "message": message });
}
});
app.get('/now', function middleware(req, res, next) {
req.time = new Date().toString(); // Hypothetical synchronous operation
next();
}, function time(req, res) {
res.send({time:req.time});
console.log(req.time)
});
app.get("/:word/echo", (req, res) => {
const { word } = req.params;
res.json({
echo: word
});
});
app.route('/name').get((req,res)=>{
const first = req.query.first
const last = req.query.last
res.json({
'name' : `${first} ${last}`
})
}).post((req,res)=>{
var string = `${req.body.first} ${req.body.last}`
res.json({
name:string
})
})
module.exports = app;