-
Notifications
You must be signed in to change notification settings - Fork 0
/
testServer.js
37 lines (33 loc) · 1.03 KB
/
testServer.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
const cors = require('cors');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const router = express.Router();
function processHeaders(req, res, next){
// console.log(`${req.method}:${req.originalUrl}\nheaders: ${JSON.stringify(req.headers, null, 2)}\nbody: ${JSON.stringify(req.body, null, 2)}`);
next();
}
router.use(express.static(`${__dirname}/`));
router.use(processHeaders, (req, res) => {
const response = {
body: req.body,
headers: req.headers,
method: req.method,
path: req.path,
query: req.query
};
// console.log(JSON.stringify(response));
res.status(200).send(JSON.stringify(response)).end();
});
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// app.use((req, _res, next) => {console.log(`req: ${req.originalUrl}`); next();});
app.use(router);
if(require.main === module){
app.listen(3000, 'localhost', () => {
console.log('Test server listening on http://localhost:3000');
});
}else{
module.exports = app;
}