Created
July 2, 2018 10:03
-
-
Save valentinconan/79b5965e8a8b7fe0531a734fd0e2f2e9 to your computer and use it in GitHub Desktop.
InterceptorNodeServer
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
| /* | |
| 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/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment