-
Notifications
You must be signed in to change notification settings - Fork 0
/
testServer.js
42 lines (36 loc) · 1.14 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
38
39
40
41
42
const cors = require('cors');
const express = require('express');
const bodyParser = require('body-parser');
const Logger = require('log-ng');
const path = require('path');
const logger = new Logger(path.basename(__filename));
const app = express();
const router = express.Router();
function processHeaders(req, res, next){
logger.info(`${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
};
logger.info(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) => {logger.debug(`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;
}