node 代理服务器示例
时间: 2023-11-17 21:06:33 浏览: 138
以下是一个使用http-proxy创建代理服务器的Node.js示例:
```javascript
const http = require('http');
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});
const server = http.createServer((req, res) => {
// 在这里可以对请求进行一些处理
// ...
// 将请求转发到目标服务器
proxy.web(req, res, { target: 'http://localhost:3000' }, (err) => {
console.error(err);
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong.');
});
});
server.listen(8080);
console.log('Proxy server listening on port 8080');
```
这个示例创建了一个代理服务器,将所有请求转发到本地的3000端口。你可以根据需要修改目标服务器的地址和端口号。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)