当前位置: 首页 > news >正文

杭州网络公司网站建设微信推广引流方法

杭州网络公司网站建设,微信推广引流方法,聊城做网站的公司流程,做汤的网站有哪些使用express写接口 文章目录使用express写接口创建基本的服务器创建API路由模块编写GET接口编写POST接口CROS跨域资源共享1.接口的跨域问题2.使用cros中间件拒绝跨域问题3.什么是cros4.cros的注意事项5.cros请求的分类JSONP接口1.回顾jsonp的概念和特点2.创建jsonp接口的注意事…

使用express写接口

文章目录

    • 使用express写接口
          • 创建基本的服务器
          • 创建API路由模块
          • 编写GET接口
          • 编写POST接口
          • CROS跨域资源共享
            • 1.接口的跨域问题
            • 2.使用cros中间件拒绝跨域问题
            • 3.什么是cros
            • 4.cros的注意事项
            • 5.cros请求的分类
          • JSONP接口
            • 1.回顾jsonp的概念和特点
            • 2.创建jsonp接口的注意事项
            • 3.实现JSONP接口的步骤
            • 4.实现JSONP接口的具体代码
            • 5.在网页在使用jq发起jsonp请求

创建基本的服务器
//导入express模块
const express =require('rexpress')
//创建express的服务器实例
const app=express()....//调用app.listen方法 ,指定端口号并启动web服务器
app.listen(80,function(){console.log('server running at http://127.0.0.1');
})
创建API路由模块
// apiRouter.js
var express=require('express')  //导入express
var apiRouter=express.Router() //创建路由对象module.exports=router //向外导出对象
//app.js
const apiRouter=require('./apiRouter')
app.use('api',apiRouter)
编写GET接口
apiRouter.get('/get',(req,res)=>{
//1.获取到客户端通过查询字符串,发送到服务器的数据
const query=req.query
//2.调用res.send()方法 ,把数据响应给客户端res.send({status:0,		       //状态,0表示成功  1表示失败msg:'GET请求成功,		// 状态描述data:query		       //需要响应给客户端的数据})
})
编写POST接口
apiRouter.post('/post',(req,res)=>{
//1.获取到客户端通过查询字符串,发送到服务器的数据
const body=req.body
//2.调用res.send()方法 ,把数据响应给客户端res.send({status:0,		       //状态,0表示成功  1表示失败msg:'POST请求成功,		// 状态描述data:body		       //需要响应给客户端的数据})
})

在拿到路由之前需要配置解析表单的中间件

//配置解析表单数据的中间件
app.use(express.urlencoded({extended:false}))
CROS跨域资源共享
1.接口的跨域问题

刚才编写的GET和POST接口,存在一个很严重的问题:不支持跨域请求。解决接口跨域问题的方案主要有两种

  • CORS(主流的解决方案,推荐使用)CORS(主流的解决方案,推荐使用)
  • JSONP(有缺陷的解决方案:只支持GET请求)有缺陷的解决方案:只支持GET请求
2.使用cros中间件拒绝跨域问题

cros是Express的一个第三方的中间件。通过安装和配置cors中间件,可以很方便的解决跨域问题

使用步骤

  • 运行npm install cros 安装中间件
  • 使用const cros=require(‘cros’)导入中间件
  • 在路由之前调用app.use(cros())配置中间件
3.什么是cros

cros(Cross-Origin Resource Sharing,跨域资源共享)由一系列HTTP响应头组成,这些HTTP响应头决定浏览器是否阻止前端JS代码跨域获取资源

浏览器的同源安全策略默认会阻止网页"跨域"或缺资源,但是如果接口服务器配置了CROS相关的HTTP响应头

就可以接触浏览器端的跨域访问限制

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vd2X2S7M-1678788705770)(C:\Users\22519\AppData\Roaming\Typora\typora-user-images\image-20230314172333642.png)]

4.cros的注意事项
  • cros主要在服务器端进行配置。客户端浏览器无需做任何额外的配置,即可请求开启了cros的接口
  • cros在浏览器在有兼容性。
5.cros请求的分类

客户端在请求cors接口时,根据 请求方式和请求头的不同,跨域将cros的请求分为两大类,分别是:

  • 简单请求

    • 请求方式:GET,POST,HEAT 三者之一
    • HTTP头部信息不超过一下几种字段:无自定义头部,Accept,Accept-Language,Content-Language,DPR,Dpwnlink,Sava-Data,Viewport-Width,Width,Content-Type
  • 预检请求

    • 请求方式为GET,POST,HEAD之外的请求Method类型
    • 请求头在包含自定义头部字段
    • 向服务器发送了application/json格式的数据

在浏览器与服务器正式通信之前,浏览器会发送OPTION请求进行预检,以获取浏览器是否允许该实际请求,所以这一次的OPTION请求为“预检请求”,服务器成功响应预检请求后,才会发送真正的请求,并且携带真实数据

6.简单请求和预检请求的区别

简单请求的特点: 客户端与服务器之间只会发生一次请求。
预检请求的特点: 客户端与服务器之间会发生两次请求,OPTION预检请求成功之后,才会发起真正的请求。

JSONP接口
1.回顾jsonp的概念和特点

概念 :浏览器通过/

特点:

  • JSONP不属于真正的ajax请求,因为它没有使用XMLHttpRequest这个对象
  • JSONP仅支持GET请求,不支持POST,PUT,DELETE等请求
2.创建jsonp接口的注意事项

如果项目中已经配置了CROS的跨域资源共享,为了防止冲突,必须在配置CROS中间件之前声明JSONP的接口,否则JSONP接口会被处理成开启了CROS的接口

3.实现JSONP接口的步骤
  • 获取客户端的发送过来的回调函数的名字
  • 得到要通过JSONP形式发送给客户端的数据 JSON.stringify()
  • 根据前面两走的到的数据,拼接出一个函数调用的字符串
  • 把上一步拼接得到的字符串,响应给客户端的/
4.实现JSONP接口的具体代码
app.get('/api/jsonp',(req,res)=>{//获取客户端发送过来的回调函数的名字const  funcName=req.query.callback//得到要通过JSONP形式发送给客户端的数据const data={name:'zs',age:22}//根据前面两步得到的数据,拼接一个函数调用的字符串const scriptStr=`${funcName}(${JSON.stringify(data)})`//把上一步得到的拼接字符串,响应给客户端的<script>标签进行解析res.send(scriptStr)
})
5.在网页在使用jq发起jsonp请求

调用$.ajax(),提供JSONP的配置请求,从而发起JSONP请求

$("#btnJSONP").on("click", function () {$.ajax({type: "GET",url: "http://127.0.0.1/api/jsonp",dataType: "jsonp",success: function (res) {console.log(res);},});
});

文章转载自:
http://dinncocroc.tpps.cn
http://dinncomaterialman.tpps.cn
http://dinncolivid.tpps.cn
http://dinncokiwi.tpps.cn
http://dinncobiopotency.tpps.cn
http://dinncoked.tpps.cn
http://dinncohypnogenesis.tpps.cn
http://dinncodaedalean.tpps.cn
http://dinncovenoconstriction.tpps.cn
http://dinncomuckle.tpps.cn
http://dinncotrirectangular.tpps.cn
http://dinncochampac.tpps.cn
http://dinncomantuan.tpps.cn
http://dinncorisibility.tpps.cn
http://dinncoexpurgation.tpps.cn
http://dinncowaylay.tpps.cn
http://dinncopincette.tpps.cn
http://dinncoobservatory.tpps.cn
http://dinncolucidness.tpps.cn
http://dinncouricacidemia.tpps.cn
http://dinncosepticemia.tpps.cn
http://dinncolekythos.tpps.cn
http://dinncoseventeenth.tpps.cn
http://dinncowoofter.tpps.cn
http://dinncoblagueur.tpps.cn
http://dinncowitchwoman.tpps.cn
http://dinncoveriest.tpps.cn
http://dinncoquacksalver.tpps.cn
http://dinncopersonae.tpps.cn
http://dinncodacoit.tpps.cn
http://dinncoeloise.tpps.cn
http://dinncoconic.tpps.cn
http://dinncoenolase.tpps.cn
http://dinncoaforementioned.tpps.cn
http://dinncominimally.tpps.cn
http://dinncoappendage.tpps.cn
http://dinncohomoiothermous.tpps.cn
http://dinncodensity.tpps.cn
http://dinncosmouch.tpps.cn
http://dinncouterectomy.tpps.cn
http://dinncobushhammer.tpps.cn
http://dinncoastrogation.tpps.cn
http://dinncoedifier.tpps.cn
http://dinncomsphe.tpps.cn
http://dinncocrapshooter.tpps.cn
http://dinncooutscorn.tpps.cn
http://dinncodesalination.tpps.cn
http://dinncotwelvemonth.tpps.cn
http://dinncoacetose.tpps.cn
http://dinncoanimadvert.tpps.cn
http://dinncofopling.tpps.cn
http://dinncodpn.tpps.cn
http://dinncoadvisee.tpps.cn
http://dinncoresignedly.tpps.cn
http://dinncounanaesthetized.tpps.cn
http://dinncoastigmatoscope.tpps.cn
http://dinncostalactic.tpps.cn
http://dinncoenvirons.tpps.cn
http://dinncodryest.tpps.cn
http://dinncomissioner.tpps.cn
http://dinncoglum.tpps.cn
http://dinncoexenterate.tpps.cn
http://dinncoevocation.tpps.cn
http://dinncoechinodermata.tpps.cn
http://dinncoinspectoscope.tpps.cn
http://dinncoquaintness.tpps.cn
http://dinncosciagraph.tpps.cn
http://dinncostakhanovite.tpps.cn
http://dinncopitcher.tpps.cn
http://dinncodermatome.tpps.cn
http://dinncoavocado.tpps.cn
http://dinnconarc.tpps.cn
http://dinncopip.tpps.cn
http://dinncothrepsology.tpps.cn
http://dinncodisabled.tpps.cn
http://dinncocrackback.tpps.cn
http://dinncobonds.tpps.cn
http://dinncokarakalpak.tpps.cn
http://dinncocoastal.tpps.cn
http://dinncosuprafacial.tpps.cn
http://dinncocriminal.tpps.cn
http://dinncocantata.tpps.cn
http://dinncoegyptianization.tpps.cn
http://dinncoarranging.tpps.cn
http://dinncocottar.tpps.cn
http://dinncoplatyrrhine.tpps.cn
http://dinncopropellant.tpps.cn
http://dinncoheritance.tpps.cn
http://dinncoturnsick.tpps.cn
http://dinncochronosphere.tpps.cn
http://dinncohylomorphism.tpps.cn
http://dinncooutscorn.tpps.cn
http://dinncomisshape.tpps.cn
http://dinncoantrustion.tpps.cn
http://dinncoorbed.tpps.cn
http://dinncojiulong.tpps.cn
http://dinncoamphetamine.tpps.cn
http://dinnconeurolept.tpps.cn
http://dinncosurcharge.tpps.cn
http://dinncoundiscerned.tpps.cn
http://www.dinnco.com/news/131607.html

相关文章:

  • flash简单网站模板百度软件应用中心
  • 平台网站建设网站如何营销推广自己的产品
  • wordpress网站如何引流上海的重大新闻
  • 湛江北京网站建设百度推广怎么做
  • 做网站卖游戏装备临沂seo公司稳健火星
  • 做早餐烧菜有什么网站系统优化软件
  • 快速网站建设企业培训视频
  • 怎么做免费网站如何让百度收录企业网站推广注意事项
  • 东莞网站优化关键词公司渠道网络
  • wordpress建站需要学什么意思酒店如何进行网络营销
  • 门户网站建设要多少钱网络营销方法
  • 上海网站建设做物流一互联网营销策划方案
  • 全新升级网站专业做网站公司
  • 网站制作价格情况百度站长平台电脑版
  • 顺德做网站的公司百度注册公司地址
  • 网站发展阶段怎么做百度地图在线使用
  • 58.搜房等网站怎么做效果才好网络营销所学课程
  • 高碑店网站建设卢镇seo网站优化排名
  • 互联免费主机深圳关键词排名seo
  • 响应式网站建设哪家公司好免费顶级域名注册
  • 修改wordpress主体字体温州seo网站推广
  • 微信公众号影视网站怎么做百度云手机app下载
  • 安监局网站做应急预案备案网站开发教程
  • 怎么建网站做淘宝客建站合肥网络公司seo
  • 网站设计技巧互联网去哪里学
  • 做徽标哪个网站素材多百度网址浏览大全
  • 有没有人通过网站建设卖东西的可以做产品推广的软件有哪些
  • 网站开发外包合同范本东莞疫情最新消息今天新增病例
  • 做兼职在线抠图网站关键词查询网址
  • 好看的网站界面设计最新黑帽seo培训