当前位置:网站首页>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?')
})
参数处理和请求头处理并不冲突,若没有请求头的话可以单纯处理参数,不用加其他种种~
边栏推荐
- Do you know the basic process and use case design method of interface testing?
- The easiest trick to support quick renaming of various files
- tf.reduce_mean() and tf.reduce_sum()
- Machine Learning Summary (2)
- 1101 B是A的多少倍 (15 分)
- 【415. 字符串相加】
- 1071 Small Gamble (15 points)
- 软件测试常用工具的用途及优缺点比较(详细)
- Hibernate 的 Session 缓存相关操作
- 【BM87 合并两个有序的数组】
猜你喜欢
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
1002 Write the number (20 points)
1046 划拳 (15 分)
Item 2 - Annual Income Judgment
项目2-年收入判断
1003 我要通过 (20 分)
初级软件测试工程师笔试试题,你知道答案吗?
【LeetCode】Summary of linked list problems
One-hot in TF
tf.cast(), reduce_min(), reduce_max()
随机推荐
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
查询跟踪快递单号物流,智能分析物流中转有延误的单号
迷你图书馆系统(对象+数组)
用 Antlr 重构脚本解释器
初级软件测试工程师笔试试题,你知道答案吗?
1076 Wifi Password (15 points)
Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
8、Mip-NeRF
麒麟V10系统打包Qt免安装包程序
The easiest trick to support quick renaming of various files
机器学习(三)多项式回归
Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤
1003 I want to pass (20 points)
装饰器模式:Swift 实现
JRS303-数据校验
【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简
string类接口介绍及应用
1101 How many times B is A (15 points)
1036 跟奥巴马一起编程 (15 分)