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

nodejs 中间件

1:全集中间件

//导入模块
const express = require('express');
//创建应用
const app = express();function logger(req, resp, next) { console.log(`${req.method} ${req.path} "${req.headers['user-agent']}"`);next();
}
app.use(logger); //注冊中間件
app.get('/',(req,resp)=>{//输出响应console.log('request coming.............');resp.send("hello world");
});app.listen(8080,()=>{console.log('listening on 8080');
});

执行结果:
GET / “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36”
request coming…
GET /.well-known/appspecific/com.chrome.devtools.json “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36”

路由中间件:

//导入模块
const express = require('express');
//创建应用
const app = express();function logger(req, resp, next) { console.log(`${req.method} ${req.path} "${req.headers['user-agent']}"`);next();
}app.get('/',logger,(req,resp)=>{ //注册到路由上//输出响应console.log('request coming.............');resp.send("hello world");
});app.listen(8080,()=>{console.log('listening on 8080');
});``
可配置中间件:

//导入模块
const express = require(‘express’);
//创建应用
const app = express();

function logger(options) {
return function (req, resp, next) {
const logs = [];
if (options.method) {
logs.push(req.method);
}
if (options.path) {
logs.push(req.path);
}
if (options[‘user-agent’]) {
logs.push(req.headers[‘user-agent’]);
}
console.log(logs.join(’ '));
next();
};
}

app.use(logger({ method: true, path: true }));
app.get(‘/’,(req,resp)=>{
//输出响应
console.log(‘request coming…’);
resp.send(“hello world”);
});

app.get(‘/user’,(req,resp)=>{
//输出响应
console.log(‘request coming…’);
resp.send(“hello user”);
});

app.listen(8080,()=>{
console.log(‘listening on 8080’);
});


**响应时长中间件**

//导入模块
const express = require(‘express’);
//创建应用
const app = express();
function responseTime(req,resp,next) {
const start = Date.now();
resp.once(‘finish’, function () {
console.log(‘处理时长’,Date.now() - start);
});
next();
}
app.use(responseTime);
app.get(‘/’,(req,resp)=>{
//输出响应
setTimeout(() => {
console.log(‘request coming…’);
resp.send(“hello world”);
},1000);

});

app.listen(8080,()=>{
console.log(‘listening on 8080’);
});


**静态资源中间件**
express.static(root,[options])

//导入模块
const express = require(‘express’);
//创建应用
const app = express();

app.use(‘/public’,express.static(‘./public’));

app.listen(8080,()=>{
console.log(‘listening on 8080’);
});

http://www.xdnf.cn/news/1319455.html

相关文章:

  • 科目二的四个电路
  • Windows运维之以一种访问权限不允许的方式做了一个访问套接字的尝试
  • 健身房预约系统SSM+Mybatis实现(三、校验 +页面完善+头像上传)
  • es7.17.x es服务yellow状态的排查查看节点,分片状态数量
  • 生成模型实战 | InfoGAN详解与实现
  • 1. Docker的介绍和安装
  • 安装pytorch3d后报和本机cuda不符
  • gitee 流水线+docker-compose部署 nodejs服务+mysql+redis
  • Matlab数字图像处理——基于BM4D压缩感知的三维图像信号重构算法
  • ai测试(六)
  • 中级统计师-会计学基础知识-第五章 财务报告
  • (MST,并查集)nflsoj #4114 货车运输/洛谷 P1967NOIP2003 货车运输
  • 反向代理、负载均衡器与API网关选型决策
  • C++算法题目分享:二叉搜索树相关的习题
  • 【165页PPT】基于IPD的研发项目管理(附下载方式)
  • RISC-V汇编新手入门
  • 计算机视觉(一):nvidia与cuda介绍
  • Android 组件封装实践:从解耦到架构演进
  • Python使用数据类dataclasses管理数据对象
  • metasploit 框架安装更新遇到无法下载问题如何解决
  • Redis面试精讲 Day 24:Redis实现限流、计数与排行榜
  • C#中List、Path、字符串操作等常用方法总结
  • ​​Vue 3 开发速成手册
  • 说一下事件传播机制
  • Python注解
  • Python入门第7课:异常处理机制:让你的程序更健壮(try-except详解)
  • 配置 NVIDIA RTX 5090 + sm_120 + flashattention,已跑通一个大模型 ~~
  • C语言(12)——进阶函数
  • Day3--滑动窗口与双指针--2461. 长度为 K 子数组中的最大和,1423. 可获得的最大点数,1052. 爱生气的书店老板
  • 数字货币的法律属性与监管完善路径探析