forked from freeCodeCamp/boilerplate-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyApp.js
62 lines (49 loc) · 1.4 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
var dotenv = require("dotenv");
dotenv.config();
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use("/public", express.static(__dirname + "/public"));
app.use("/", function(req, res, next) {
console.log(req.method + " " + req.path + " " + " - " + req.ip);
next();
});
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/now", function(req, res, next) {
req.time = new Date().toString();
next();
}, function(req, res) {
res.json({
time: req.time
});
});
app.get("/", function(req, res) {
absolutePath = __dirname + "/views/index.html"
res.sendFile(absolutePath);
});
app.get("/json", function(req, res) {
raw = "Hello json";
message = (process.env.MESSAGE_STYLE == 'uppercase') ? raw.toUpperCase() : raw;
res.json({
message: message,
});
});
app.get("/:word/echo", function(req, res) {
res.json({
echo: req.params.word
})
});
app.route("/name").get(function(req, res) {
first_name = (req.query["first"] == undefined) ? '' : req.query["first"]
last_name = (req.query["last"] == undefined) ? '' : req.query["last"]
res.json({
name: first_name + " " + last_name
});
}).post(function(req, res) {
first_name = (req.body.first == undefined) ? '' : req.body.first
last_name = (req.body.last == undefined) ? '' : req.body.last
res.json({
name: first_name + " " + last_name
});
});
module.exports = app;