//这个地址在window上用ipconfig查看,mac/linux用ifconfig查看
case 'bbs.aaaa.com':
proxy.web(req, res, { target: 'http://127.0.0.1:3000' }); //转到这个地址
break;
default:
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Welcome to my server!');
}
});
console.log("listening on port 8080")
server.listen(8080);
node proxy.js 以后启动了proxy服务器.可以通过电脑的ip地址访问127.0.0.1的api路由了。
如果是使用nginx也可以达到要求,在mac上使用homebrew包管理相当方便
bash下 安装 brew install nginx
启动 brew services start nginx
如果安装了atom编辑器
bash在 直接 atom /usr/local/etc/nginx/nginx.conf 打开配置文件本分以后做出修改
下面是nginx.conf的配置文件
//nginx.conf #原来的文件另存后。直接使用下面内容替换nginx.conf的内容
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 8080; #监听80880端口
server_name www.penguu.com 192.168.1.100; #这里是真机要访问的地址。
# Mac 通过终端 ifconfig 查看。比如我查看的就是192.168.1.100
#手机访问的接口就是 192.168.1.100:8080
#实际在23行监听的端口可以是80端口,由于浏览器默认就是80端口。但是在mac中有权限问题。所#以就使用8080端口
# address_book中的service中的地址也要修改路径是
# view/service.js->host,修改为 192.168.1.100:8080
#access_log /var/log/nginx/test.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://127.0.0.1:3000; # address_book的 server地址,就是本地node.js服务器的ip地址
#node.js默认就是127.0.0.1 ,port:3000是在app.js中设定的。可以修改。
}
}
}









