Skip to content

Instantly share code, notes, and snippets.

@Hiestaa
Last active September 20, 2019 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hiestaa/e642cd5762c6efda882b5c758c710a7c to your computer and use it in GitHub Desktop.
Save Hiestaa/e642cd5762c6efda882b5c758c710a7c to your computer and use it in GitHub Desktop.
Minimalist http echo server. Replies with the method, url, query and (json) body received on any endpoint. Run with `npm install && npm start`.
var express = require("express"),
app = express();
var bodyParser = require('body-parser');
function echo(req, res, next) {
const queryStr = Object.entries(req.query).map(([key, val]) => `${key}=${val}`).join('&');
const bodyStr = req.body ? JSON.stringify(req.body) : null;
const response = req.method + ' ' + req.url + (queryStr && queryStr.length ? '?' + queryStr : '') + '\n' + (bodyStr ? bodyStr + '\n' : '');
console.log(response);
res.end(response);
}
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.get("/*", echo);
app.post("/*", echo);
app.put("/*", echo);
app.delete("/*", echo);
app.head("/*", echo);
app.options("/*", echo);
app.patch("/*", echo);
console.log("Echo server listening on localhost:3000");
app.listen(3000);
{
"name": "echo",
"version": "1.0.0",
"description": "Minimalist http echo server. Replies with the method, url, query and (json) body received on any endpoint. ",
"main": "echo.js",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node echo.js"
},
"keywords": [
"http",
"echo",
"server"
],
"author": "Romain GUYOT de la HARDROUYERE",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment