The following code implements the function as follows:
First, create an HTTP server. When the server receives the client's request, it requests data from the "www.taobao.com" website. After the response data received from the website, it sends the response data to the client.
The code copy is as follows:
var http=require("http");
var url=require("url");
var server=http.createServer(function(sreq,sres){
var url_parts=url.parse(sreq.url);
var opts={
host:"www.taobao.cn",
port:80,
path:url_parts.pathname,
headers:sreq.headers
};
var creq=http.get(opts, function (cres) {
sres.writeHead(cres.statusCode,cres.headers);
cres.pipe(sres);
});
sreq.pipe(creq);
});
server.listen(1337,"127.0.0.1", function () {
console.log("Start listening"+server.address().port+"...");
});
After running the code, run the program on the browser side:
I found that the interface is Taobao’s official website, but the address has indeed become our local one.
Isn't it fun? In fact, node.js can do a lot of things, so friends can develop them yourself.