-
Notifications
You must be signed in to change notification settings - Fork 0
/
myApp.js
79 lines (58 loc) · 1.48 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
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
require('dotenv').config()
console.log("Hello World");
const middle = (req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next()
}
app.use(middle);
app.get("/", (req, res) => {
// res.send("Hello Express");
let absPath = __dirname + "/views/index.html";
// console.log(absPath);
res.sendFile(absPath);
//res.sendFile(absPath, (error) => {
// if (error) {
// next(error)
// } else {
// console.log("enviado:", absPath)
// }
// })
});
app.get("/json", (req, res, next) => {
if(process.env.MESSAGE_STYLE === "uppercase") {
res.json({"message": "Hello json".toUpperCase()})
} else {
res.json({"message": "Hello json"})
}
});
app.get('/now', (req, res, next) => {
req.time = new Date().toString();
next();
},
(req, res) => {
res.json({time: req.time})
}
)
app.get('/:word/echo', (req, res) => {
const { word } = req.params;
console.log(word);
res.json({echo: word});
});
app.get('/name', (req, res) => {
console.log(req.path, req.query)
let name = req.query;
let firstname = name.first;
let lastname = name.last;
res.json({name: `${firstname} ${lastname}`})
});
app.use(bodyParser.urlencoded({extended: false}));
app.post('/name', (req, res) => {
let string = req.body.first + " " + req.body.last;
console.log(string);
res.json({name: string});
});
app.use('/public', express.static(__dirname + "/public"));
module.exports = app;