/* npm install http npm install fs npm install path npm install xml npm install querystring */ var http = require('http'), fileSystem = require('fs'), path = require('path'), xml = require('xml'), qs = require('querystring'); http.createServer(function(req, res) { console.log('ok starting'); console.log('Server running at '+req.url ); try { let contentType = req.headers['content-type']; res.writeHead(200, { 'Content-Type': contentType, 'Access-Control-Allow-Origin':'*', 'Access-Control-Allow-Methods':'GET, POST, OPTIONS, PUT, DELETE' }); let body = []; req.on('error', (err) => { console.error(err); }).on('data', (chunk) => { body.push(chunk); }).on('end', () => { body = Buffer.concat(body).toString(); if(body!=''){ if(contentType==="application/json"){ console.log(JSON.stringify( { "headers": req.rawHeaders, "body":JSON.parse(body) } )); res.end(JSON.stringify( { "headers": req.rawHeaders, "body":JSON.parse(body) } )); }else if(contentType==="application/xml" || contentType==="text/xml"){ console.log( { "headers": req.headers, "body":body.toString() } ); res.end(xml( { "root":{ "headers": req.headers, "body":body.toString() } } )); }else{ console.log( { "headers": req.rawHeaders, "body":body } ); res.end("{headers: "+req.rawHeaders+","+"body:"+body+"}"); } }else{ if(contentType==="application/json"){ console.log(JSON.stringify( { "headers": req.rawHeaders } )); res.end(JSON.stringify( { "headers": req.rawHeaders } )); }else if(contentType==="application/xml" || contentType==="text/xml"){ console.log(JSON.stringify( { "headers": req.rawHeaders } )); res.end(xml( { "headers": req.rawHeaders } )); } else{ console.log(JSON.stringify( { "headers": req.rawHeaders } )); res.end("{headers:"+req.rawHeaders+"}"); } } }); } catch (err) { console.log(err); res.writeHead(500, { 'Content-Type': 'text/html', 'Content-Length': 0 }); throw new Error('something bad happened conf must be http://localhost:3001/'); } }) .listen(3001); console.log('Server running. at http://127.0.0.1:3001/');