![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e7cc37730cb91a4150fd92c7754a4cd4.png#pic_center)
nginx解决跨域地址
1.node安装
菜鸟教程详细安装步骤
2.初始化——任选一种
npm i express --save
npm i axios --save
可选:
https请求被拦截 :npm i request --save
表单提交,文件上传:npm i formidable --save
post请求参数:npm i body-parser --save
cnpm i express --save
cnpm i axios --save
可选:
https请求被拦截 :cnpm i request --save
表单提交,文件上传:cnpm i formidable --save
post请求参数:cnpm i body-parser --save
yarn add express
yarn add axios
可选:
https请求被拦截 :yarn add request
表单提交,文件上传:yarn add formidable
post请求参数:yarn add body-parser
3.主体代码
$.ajax({
url: 'http://localhost:3333/getNoParam',
type: 'get',
success: function(res){
res = JSON.parse(res)
console.log(res)
}
})
$.ajax({
url: 'http://localhost:3333/getHasParam',
type: 'get',
data: {param: 'test'},
success: function(res){
res = JSON.parse(res)
console.log(res)
}
})
$.ajax({
url: 'http://localhost:3333/postHasParam',
type: 'post',
data: {param: 'test'},
success: function(res){
res = JSON.parse(res)
console.log(res)
}
})
const express = require("express");
const axios = require("axios");
const app = express();
const request = require("request");
var formidable = require('formidable')
app.use(bodyParser.urlencoded({extended: true}))
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header('Access-Control-Allow-Credentials', true);
res.header("X-Powered-By", ' 3.2.1');
next();
});
app.get("/getNoParam", async (req, res) => {
const result = await axios.get('地址')
});
app.get("/getHasParam", async (req, res) => {
console.log(req.query)
const result = await axios.get('地址', {参数1:值1,...})
});
app.post("/postNoParam", async (req, res) => {
console.log(req.body)
const result = await axios.post('地址', {参数1:值1,...})
});
app.post("/getNotice", async (req, res) => {
var form=new formidable.IncomingForm(req.url)
form.parse(req, async function (err,fields,files) {
if(err) throw err;
request({
url: '地址',
method: "GET",
headers: {
'Accept': 'application/json'
},
strictSSL: false
},(error,res,body)=>{
if (error) {
return console.log(error)
}
console.log(body)
})
})
});
app.listen(3333, () => {
console.log("本地代理服务器运行在3333端口");
});