Last active
September 20, 2019 19:02
-
-
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`.
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
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); |
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
{ | |
"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