Created
May 9, 2017 00:28
-
-
Save mmattozzi/3a8abfa9de7c61f3e87d906b20063fc4 to your computer and use it in GitHub Desktop.
HTTP echo server using node.js and express
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": "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" | |
} | |
} |
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'); | |
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); |
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
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.