node 后端代理服务器
使用request进行后端代理服务器.
通过一个.js文件创建的服务器向另一个js文件创建的服务器发送请求数据.
server1.js
var http = require('http');
var app = http.createServer(function(req,res)
{
if(req.url === '/')
{
http.get('http://localhost:8000/test',function(data)
{
var str = '';
data.on('data',function(clumh)
{
str += clumh;
});
data.on('end',function()
{
res.end(str);
});
})
}
});
app.listen(3000,function()
{
console.log('server1 is ok');
});
server2.js
var http = require('http');
var app = http.createServer(function(req,res)
{
if(req.url == '/test')
{
res.end('success!');
}
});
app.listen(8000,function()
{
console.log('server2 is ok');
});
将两个文件的终端都进行启动,再在页面输入http://localhost:3000/,就可以通过3000端口页面访问8000端口的页面内容了.