Create a Fast Server
Has it always seemed a little hard to you to see what’s actually getting sent to your web service? It has to me. There are some great tools like wireshark from https://www.wireshark.org/ that will help, but these break down when you are doing testing the way you want to do it–all on your localhost.
To make it easier, I decided to create a quick and dirty utility to do that for me using node.js.
If you’ve never used node.js, no problem. You will need to get it running by following the tutorial here. Once that’s done, we can begin our testing.
To begin, I’m going to create a quick form that will communicate with by fake web service. Here it is:
<form action="http://localhost:8093/update" method="post">
First name:
<input name="firstname" type="text" value="Ford" />
Last name:
<input name="lastname" type="text" value="Prefect" />
<input type="submit" value="Submit" />
I saved this in a file called form.html. This form runs a POST against port 8093. You can change this to accommodate your testing needs.
Create a Fast Server
Next, we need our simple server. Here’s the code:
var cors = require('cors');
var express = require('express');
var http = require('http');
var url = require('url');
var app = express();
app.options('*', cors());
app.use(cors());
app.listen(8093, function () {
console.log('CORS-enabled web server listening on port 8093');
});
app.post('/update', function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
var q = url.parse(request.url, true);
console.log("Host:" + q.host);
console.log("Path:" + q.pathname);
console.log("Search:" + q.search);
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
console.log("Body:" + body);
});
response.end("Complete!");
});
This is a pretty simple server that simply takes the contents of the request and dumps it to the screen. This is more than the basic server to handle CORS origin requests.
I saved this as fakeserver.js. Before running this, you will need to install a couple of modules:
npm install express
npm install cors
After doing this, you can start your server by running:
node fakeserver.js
Now go back to your browser and hit submit. Your output should look like this:
Host:null
Path:/update
Search:
Body:firstname=Ford&lastname=Prefect
Wasn’t that simple? Just took a couple of minutes and you’re in business.
Happy coding!