-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (50 loc) · 1.44 KB
/
index.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 express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use((req, res, next) => {
res.set('Cache-Control', 'no-cache, no-store');
next();
});
/**
* Log request info to console with colors.
* */
app.use((req, res, next) => {
const requestMethod = req.method ? req.method : '';
console.log(`\n\x1b[33m${requestMethod} \x1b[32mRequest at: `, new Date().toLocaleString());
console.log('\x1b[32m \nRequest headers: \x1b[0m', req.headers);
console.log('\x1b[32m \nRequest cookies: \x1b[0m', req.cookies);
console.log('\x1b[32m \nRequest query: \x1b[0m', req.query);
// console.log('\x1b[32m \nRequest body: \x1b[0m', req.body);
console.log('\n\x1b[31m ----------- NEXT ----------- \x1b[0m');
console.log(`\n\n\n\n${req.protocol + '://' + req.get('host') + req.originalUrl}\n\n\n\n`)
next();
});
/**
* Echo request body as JSON
* */
app.all('/echo', (req, res) =>{
res.json(req.body);
});
/**
* Echo headers as JSON
* */
app.all('/echo/headers', (req, res) =>{
res.json(req.headers);
});
/**
* Echo query params as JSON
* */
app.get('/echo/query', (req, res) =>{
res.json(req.query);
});
/**
* Echo headers as JSON
* */
app.all('*', (req, res) =>{
res.json(req.headers);
});
module.exports = app;