Skip to content

Instantly share code, notes, and snippets.

@benzapier
Last active November 18, 2019 23:01
Show Gist options
  • Select an option

  • Save benzapier/4be596208c9a3dd67325b7fbc72b8a0a to your computer and use it in GitHub Desktop.

Select an option

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.
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!'));
@benzapier
Copy link
Author

FYI - you may need to run npm install express before this will run on your system.

@mike-solomon
Copy link

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