别跟后台墨迹了,我们自己来——node.js解决跨域

在这里插入图片描述

nginx解决跨域地址

1.node安装

菜鸟教程详细安装步骤

2.初始化——任选一种

  • npm安装
npm i express --save
npm i axios --save
可选:
	https请求被拦截 :npm i request --save
	表单提交,文件上传:npm i formidable --save
	post请求参数:npm i body-parser --save
  • cnpm安装
cnpm i express --save
cnpm i axios --save
可选:
	https请求被拦截 :cnpm i request --save
	表单提交,文件上传:cnpm i formidable --save
	post请求参数:cnpm i body-parser --save
  • yarn安装
yarn add express
yarn add axios
可选:
https请求被拦截 :yarn add request 
 表单提交,文件上传:yarn add formidable
 post请求参数:yarn add body-parser

3.主体代码

  • 前台jquery.ajax请求
// 没有参数的get请求
  $.ajax({
    url: 'http://localhost:3333/getNoParam',
    type: 'get',
    success: function(res){
      res = JSON.parse(res)
      console.log(res)
    }
  })
  // 有参数的get请求
  $.ajax({
    url: 'http://localhost:3333/getHasParam',
    type: 'get',
    data: {param: 'test'},
    success: function(res){
      res = JSON.parse(res)
      console.log(res)
    }
  })
	// 有参数的post请求
  $.ajax({
    url: 'http://localhost:3333/postHasParam',
    type: 'post',
    data: {param: 'test'},
    success: function(res){
      res = JSON.parse(res)
      console.log(res)
    }
  })
  • node.js代码

const express = require("express");
const axios = require("axios");
const app = express();
// 可选
// https请求
const request = require("request");
// 表单提交
var formidable = require('formidable')
// post参数
app.use(bodyParser.urlencoded({extended: true}))

//添加CORS头
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();
});
 //get没参数
app.get("/getNoParam", async (req, res) => { // 接收前台请求
	// 请求后台地址
	const result = await axios.get('地址')
});
 //get有参数
app.get("/getHasParam", async (req, res) => { // 接收前台请求
	console.log(req.query) // 获取参数
	// 请求后台地址
	const result = await axios.get('地址', {参数1:值1,...})
});
 //post有参数
app.post("/postNoParam", async (req, res) => { // 接收前台请求
  	console.log(req.body) // 获取参数
	// 请求后台地址
	const result = await axios.post('地址', {参数1:值1,...})
});
// https请求

//获取通知
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
        // body: 参数
      },(error,res,body)=>{
          if (error) {
              return  console.log(error)
          }
          console.log(body)
      })
  })
});
app.listen(3333, () => {
    console.log("本地代理服务器运行在3333端口");
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值