Skip to content

Instantly share code, notes, and snippets.

@mmattozzi
Created May 9, 2017 00:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mmattozzi/3a8abfa9de7c61f3e87d906b20063fc4 to your computer and use it in GitHub Desktop.
Save mmattozzi/3a8abfa9de7c61f3e87d906b20063fc4 to your computer and use it in GitHub Desktop.
HTTP echo server using node.js and express
{
"name": "mock-http-server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.text({
type: function(req) {
return 'text';
}
}));
app.post('/post', function (req, res) {
console.log(req.body);
res = res.status(200);
if (req.get('Content-Type')) {
console.log("Content-Type: " + req.get('Content-Type'));
res = res.type(req.get('Content-Type'));
}
res.send(req.body);
});
http.createServer(app).listen(7000);
@mmattozzi
Copy link
Author

It's actually a little tricky to have express not try to do something with the POST body of a request. I had to use the bodyParser.text function and define a custom type that evaluates all incoming content as text thus ending around usage of the type-is library.

@judavi
Copy link

judavi commented Sep 5, 2019

Thanks for sharing! it works for me to help to identify an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment