Skip to content

Instantly share code, notes, and snippets.

@daithiw44
Created April 26, 2017 09:01
Show Gist options
  • Save daithiw44/9dd2412642286028feb5a30460a16b65 to your computer and use it in GitHub Desktop.
Save daithiw44/9dd2412642286028feb5a30460a16b65 to your computer and use it in GitHub Desktop.
Proxy Server for UI Design Team
'use strict';
let httpServer
, http = require('http')
, path = require('path')
, url = require('url')
, fs = require('fs');
let port = process.env.www_port || 3000;
let mimeTypes = {
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"js": "text/javascript",
"css": "text/css"};
// httpServer = http.createServer(function(req, res) {
httpServer = http.createServer((req, res) => {
let proxy, filename, body, urlReq, options, arrURL, readStream, mimeType;
if (req.url === '/') {
req.url = '/login.html';
}
body ='';
arrURL = req.url.split('/');
req.addListener('data',(chunk)=>{
body += chunk;
});
req.addListener('error',(error)=>{
//next(err);
});
req.addListener('end',(chunk)=>{
if (chunk) {
body += chunk;
}
});
if( req.url.split('/')[1] === 'xxx'){
// if( req.url.split('/')[1] === 'UNOUKBNDEVReportsWCF'){
urlReq = req.url.replace(/xxx/, 'yyy');
options = {
//hostname: 'localhost',
hostname: '192.168.21.50',
port: 80,
path: urlReq,
method: req.method,
headers: req.headers,
body: body,
};
//debugger;
proxy = http.request(options, (resp)=> {
res.writeHead(200,resp.headers);
resp.pipe(res, {
end: true
});
});
req.pipe(proxy, {
end: true
});
} else if (req.url !== '/favicon.ico') {
filename = (__dirname+ req.url.split('?')[0]);
console.log(filename);
fs.access(filename, function (err) {
mimeType = mimeTypes[path.extname(filename).split(".").reverse()[0]];
if(err){
res.writeHead(404, {'Content-Type': mimeType} );
//res.statusCode = 404;
res.end();
} else {
res.writeHead(200, {'Content-Type': mimeType} );
readStream = fs.createReadStream(filename);
readStream.pipe(res);
}
});
}
}).listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment