-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (29 loc) · 983 Bytes
/
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
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = Number(process.env.PORT) || 4001
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(function (req, res, next) {
if (req.path === '/favicon.ico') return next()
res.setHeader('Content-Type', 'text/plain')
switch (req.method) {
case 'GET':
case 'HEAD':
console.log(req.method, req.query)
res.end(JSON.stringify(req.query, null, 2))
break
case 'POST':
console.log(req.method, req.body)
res.end(JSON.stringify(req.body, null, 2))
break
}
})
// prevent the app from starting up durings tests
if (!module.parent) {
// workaround for https://github.com/expressjs/express/issues/1101
const server = require('http').createServer(app)
server.listen(port, () => console.log(`app running on http://localhost:${port}`))
.on('error', () => server.close())
}
module.exports = app