1,下載nginx
2,解壓后打開conf/nginx.conf 修改配置
server { listen 8841;#監聽端口 server_name localhost;#代理服務地址 location / { #默認訪問 root html; index index.html index.php 1.php 1.html; } #開始配置我們的反向代理 location /api{ #"/api"中的api可以替換為自定義的任何內容 rewrite ^/api/(.*)$ /$1 break; include uwsgi_params; proxy_pass https://api.ioser.net; #我們要反向代理的地址,這里以本地的tomcat服務器為例 charset utf-8; #顯示中文 add_header 'Access-Control-Allow-Origin' '*'; #允許來自所有的訪問地址 add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, DELETE, OPTIONS'; #支持請求方式 add_header 'Access-Control-Allow-Headers' 'Content-Type,*'; } }
3,請求數據的時候用localhost:8841(監聽的端口號)代理需要請求數據的地址,例如:
var url1 = "http://localhost:8841/api/api/get_phone_info?access_token=UQbC9X4iqAt7YXfli9IELpogAr2KjkLx&phone=18824678858"; fetch(url1).then(res=>{ return res.json() }).then(data=>{ console.log(data) })
二。創建本地服務器
在nginx 目錄下輸入命令:
npm install express
安裝 express模塊,
2,在跟目錄下新建一個server.js,寫入代碼即可
const express = require('express') const app = express() const port = 9000 app.get('/', (req, res) => { res.send({ code:200, msg:"請求成功", data:{ text:"123" } }) }) app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
send 里面就是服務器返回的數據
3,使用命令
node server.js
開啟服務器即可