Last active
November 18, 2019 23:01
-
-
Save benzapier/4be596208c9a3dd67325b7fbc72b8a0a to your computer and use it in GitHub Desktop.
Simple webserver that runs and replies with a `200` request to anything/everything.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const app = express(); | |
| const bodyParser = require('body-parser'); | |
| // https://stackoverflow.com/questions/50908120/ | |
| const rawBodySaver = function (req, res, buf, encoding) { | |
| if (buf && buf.length) { | |
| req.rawBody = buf.toString(encoding || 'utf8'); | |
| } | |
| } | |
| app.use(bodyParser.json({ verify: rawBodySaver })); | |
| app.use(bodyParser.urlencoded({ verify: rawBodySaver, extended: true })); | |
| app.use(bodyParser.raw({ verify: rawBodySaver, type: '*/*' })); | |
| const printRequest = (req) => { | |
| const now = new Date(); | |
| console.log(now, now.getTime()); | |
| console.log(req.method, req.url); | |
| if (Object.keys(req.query).length > 0) { | |
| console.log('Query params ==============') | |
| console.log(req.query); | |
| } | |
| console.log('Headers =================='); | |
| console.log(req.headers); | |
| if (req.rawBody) { | |
| console.log('Raw Body =================='); | |
| console.log(req.rawBody); | |
| } | |
| if (Object.keys(req.body).length > 0) { | |
| console.log('Parsed (JSON/Form/etc) Body =================='); | |
| console.log(req.body); | |
| } | |
| }; | |
| app.get('*', (req, res) => { | |
| printRequest(req); | |
| res.send('{"message": "Thanks!"}'); | |
| }); | |
| app.post('*', (req, res) => { | |
| printRequest(req); | |
| res.send('{"message": "Thanks!"}'); | |
| }); | |
| app.listen(12345, () => console.log('Example app listening on port 12345!')); | |
Author
Note - you can run the server after you do the above install by running the command node return200.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI - you may need to run
npm install expressbefore this will run on your system.