-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyApp.js
60 lines (50 loc) · 1.43 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
const bodyParser = require('body-parser');
require('dotenv').config();
var express = require('express');
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
console.log('Hello World');
app.use('/public', express.static(__dirname + '/public'));
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
})
/*
app.get('/', (req, res) => {
res.send("Hello Express");
})*/
app.get('/', (req, res) => {
const path = __dirname + "/views/index.html"
console.log(`path=${path}`);
res.sendFile(path);
})
/*
app.get('/json', (req, res) => {
res.json({"message": "Hello json"});
})*/
app.get('/json', (req, res) => {
res.json( {"message": process.env.MESSAGE_STYLE === 'uppercase' ? "HELLO JSON" : "Hello json"});
})
app.get('/now', (req, res, next) => {
req.time = new Date().toString();
next();
}, function(req, res) {
res.json({ time: req.time })
})
app.get('/:word/echo', (req, res) => {
const word = req.params.word;
res.json({echo: word});
})
app.get('/name', (req, res) => {
console.log(req.query);
// http://localhost:3000/name?first=Joe&last=Doe
res.json({name: `${req.query.first} ${req.query.last}`});
})
app.post('/name', (req, res) => {
// console.log(req.query);
// console.log(req.body);
// http://localhost:3000/library?firstname=John&lastname=Doe
res.json({name: `${req.body.first} ${req.body.last}`});
})
// http://localhost:3000
module.exports = app;