var express = require('express'); var router = express.Router(); // catch the "readData.php" url router.get('/readData.php', function(req, res, next) { if (request.method === 'POST') { // first collect the POST data var body = ''; request.on('data', function(chunk) { body += chunk.toString(); // convert Buffer to string }); request.on('end', function() { // POST data complete // now create client socket to WebJson // console.log(body); var client = new net.Socket(); var respBody = ''; client.on('data', function(data) { // handle response data respBody += data.toString(); // convert Buffer to string var index = respBody.indexOf("\x03"); if (index > -1) { // send response response.writeHead(200, {'Content-Type': 'application/json'}); response.end(respBody.substring(0, index)); client.destroy(); // kill client } }); client.on('error', function(err) { // send error response response.writeHead(500, {'Content-Type': 'text/plain'}); response.end(err.toString()); client.destroy(); // kill client }); client.setTimeout(5000, function() { // send error response response.writeHead(500, {'Content-Type': 'text/plain'}); response.end('Timeout'); client.destroy(); // kill client }); client.connect(8080, '127.0.0.1', function() { // send request client.write(body + "\x03"); }); }); return; } } module.exports = router;