Created
November 20, 2014 11:32
-
-
Save batazor111/acde8d73aea13a9e545a to your computer and use it in GitHub Desktop.
server.js
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Hello</title> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<h1>http-proxy???</h1> | |
<!-- FORM URL --> | |
<!-- <form id="form_id" action="index.html" method="post"> --> | |
<input type="text" id="url"> | |
<input type="submit" name="submit" value="Send URL"> | |
<!-- </form> --> | |
<!-- FRAME PAGE --> | |
<iframe src="http://nodejs.org/" width="100%" height="400px"></iframe> | |
<!-- LOGS --> | |
<div id="logs"> | |
<h2>Logs:</h2> | |
</div> | |
<!-- CLIENT SCRIPT --> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
$('input[type="submit"]').on('click', function(){ | |
var socket = io('http://localhost:3000'); | |
socket.emit('test_url', { url: $('#url').val() }); | |
socket.on('test_url', function(data){ | |
console.log(data); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains 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
var app = require('http').createServer(handler) | |
var io = require('socket.io')(app); | |
var fs = require('fs'); | |
app.listen(3000); | |
var _res; | |
function handler (req, res) { | |
_res = res | |
fs.readFile(__dirname + '/public/index.html', | |
function (err, data) { | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading index.html'); | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
} | |
io.on('connection', function (socket) { | |
socket.on('test_url', function(data) { | |
socket.emit('test_url', urlValidate(data.url)) | |
// Выполняем http.get | |
http.get("http://www.google.com/index.html", function(_res) { | |
console.log("Got response: " + _res.statusCode); | |
}) | |
}); | |
}); | |
function urlValidate(url){ | |
// Получает url из формы | |
// Разбиваем url на составные части | |
// и помещаем в объект options | |
var _url = url.split('/'); | |
var _host = _url[0].split(':'); | |
_url[0] = ''; | |
_path = '/' + _url.join('/').substring(1); | |
var host = _host[0] || '127.0.0.1'; | |
var port = _host[1] || 80; | |
var path = _path; | |
var options = { | |
hostname: host, | |
port: port, | |
path: path, | |
method: 'POST' | |
}; | |
return options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment