Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Install Dependencies:
- npm install http-proxy colors connect util --save
- */
- var util = require('util'),
- colors = require('colors'),
- http = require('http'),
- connect = require('connect'),
- httpProxy = require('http-proxy');
- //
- // Basic Connect App
- //
- connect.createServer(
- function (req, res, next) {
- var _write = res.write;
- res.write = function (data) {
- _write.call(res, data.toString().replace("Ruby", "nodejitsu2"));
- }
- next();
- },
- function (req, res) {
- console.log('host: ' + req.headers['host'] + '; url ' + req.url);
- if (req.headers['host'] === "stackoverflow.com") {
- proxy.web(req, res, { target: 'http://localhost:9013' });
- } else {
- proxy.web(req, res, { target: req.url });
- }
- proxy.on('error', function (err, req, res) {
- if (!res.headerSent) {
- res.writeHead(500, {
- 'Content-Type': 'text/plain'
- });
- }
- console.log(err);
- res.end('Something went wrong. And we are reporting a custom error message.');
- });
- }
- ).listen(8013);
- //
- // Basic Http Proxy Server
- //
- var proxy = httpProxy.createProxyServer({
- // target: 'http://localhost:9013'
- });
- //
- // Target Http Server
- //
- http.createServer(function (req, res) {
- res.writeHead(200, { 'Content-Type': 'text/plain' });
- res.end('Hello, I know Ruby\n');
- }).listen(9013);
- util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8013'.yellow);
- util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9013 '.yellow);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement