当前位置: 首页 > 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.lryc.cn/news/623831.html

相关文章:

  • gitee 流水线+docker-compose部署 nodejs服务+mysql+redis
  • 【计算机网络面试】TCP/IP网络模型有哪几层
  • Matlab数字信号处理——基于最小均方误差(MMSE)估计的自适应脉冲压缩算法复现
  • ThinkPHP8学习篇(三):控制器
  • 7.Ansible自动化之-实施任务控制
  • 最优化:建模、算法与理论|02 Optimization Modeling and Typical Examples(1)
  • [优选算法专题二滑动窗口——将x减到0的最小操作数]
  • 【adb端口5555】烽火hg680-gy_烽火hg680-gc安卓9线刷烧录包 解决用一段时间就提示升级的问题
  • Shell脚本-for循环语法结构
  • 【前端基础】19、CSS的flex布局
  • 蓝凌EKP产品:JSP 性能优化和 JSTL/EL要点检查列表
  • rt-thread audio框架移植stm32 adc+dac,用wavplayer录音和播放
  • 【从零开始学习Redis】项目实战-黑马点评D2
  • scikit-learn/sklearn学习|多任务套索回归MultiTaskLasso解读
  • Windows_Server软件定义网络架构
  • 【Linux系列】如何在 Linux 服务器上快速获取公网
  • 每日两道算法题:DAY3
  • uniappx 安卓端本地打包的一些总结
  • 【位运算】查询子数组最大异或值|2693
  • CNV检测--单细胞空间vs基因组WGS/WES
  • AutoSar BSW介绍
  • 《Nursing Research》(护理 SCI)LaTeX 模板详细教程:从入门到投稿(二)
  • http工作流程
  • 数据电台询价的询价要求
  • 数据链路层(1)
  • FX10/20 (CYUSB401X)开发笔记5 固件架构
  • 基于Netty的高并发WebSocket连接管理与性能优化实践指南
  • prototype 和 _ _ proto _ _的关联
  • multiboot 规范实践分析
  • 交叉编译 手动安装 SQLite 库 移植ARM