当前位置:网站首页>nodejs微服务中跨域,请求,接口,参数拦截等功能
nodejs微服务中跨域,请求,接口,参数拦截等功能
2022-08-11 07:59:00 【进击的小民工】
首先说跨域:
前端传输自定义Header
this.ajax({
method: 'GET',headers: {
myToken: global.myToken},url: url,data: data})).then(response=>{
if(typeof response === 'string'){
response = JSON.parse(response);
}
return response
})
注意:myToken为自定义名称,需要在后端指定通过,不然node会拒绝接受,如果不带headers,其实也不会跨域,但是带了请求头后如果后端不做以下操作,就会跨域
后端NodeJS获取自定义Header
通过req.headers.myToken即可获得自定义的头部数据
后端跨域处理
在跨域的情况下,header是限制传输的,因此,首先,需要在后端允许自定义Header
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With,myToken");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.all(‘*’,function… 这点代指全部接口
**next()**调用后是放行,否则不会通过任何接口
以此类推,再来说参数拦截
可以从 req.query 中取,比如:
app.all("*", function (req, res, next) {
getSign(req.query.xxx == xxx) ? next(): send('500?')
})
参数处理和请求头处理并不冲突,若没有请求头的话可以单纯处理参数,不用加其他种种~
边栏推荐
猜你喜欢
4.1 - Support Vector Machines
几何EX3 功夫牛宣布停售,入门级纯电产品为何总成弃子
Four operations in TF
The growth path of a 40W test engineer with an annual salary, which stage are you in?
机器学习(二)线性回归
租房小程序
[Recommender System]: Overview of Collaborative Filtering and Content-Based Filtering
The most complete documentation on Excel's implementation of grouped summation
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
随机推荐
2022-08-10:为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机, 游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1, 初始有一个小球在编号 0 的弹簧处。若小球在编号为 i 的弹
TF中使用softmax函数;
Kaldi语音识别工具编译问题记录(踩坑记录)
The growth path of a 40W test engineer with an annual salary, which stage are you in?
【LeetCode】Summary of linked list problems
1046 划拳 (15 分)
Dynamic Agent Learning
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
1061 True or False (15 points)
快速幂,逆元的求解
【415. 字符串相加】
C语言操作符详解
jenkins简单使用
租房小程序
FPGA 20个例程篇:11.USB2.0接收并回复CRC16位校验
My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
项目1-PM2.5预测
1051 Multiplication of Complex Numbers (15 points)
Four startup modes of Activity
2021-08-11 For loop combined with multi-threaded asynchronous query and collect results