当前位置: 首页 > 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.dtcms.com/a/335786.html

相关文章:

  • 【Jenkins】01 - Jenkins安装
  • C语言网络编程TCP通信实战:客户端↔服务器双向键盘互动全流程解析
  • [GWCTF 2019]枯燥的抽奖
  • Java线程的6种状态和JVM状态打印
  • [TryHackMe]Brainpan1( 内存溢出)
  • PERCEIVER IO:一种用于结构化输入与输出的通用架构
  • 脉冲计数实现
  • 深入剖析ROS参数服务器通信机制 ——共享全局数据的“云端仓库”实现原理
  • Node.js安装教程
  • MySQL的事务日志:
  • java之 junit4单元测试Mockito的使用
  • 26. Object.defineProperty 和 Proxy 用法
  • 中级统计师-会计学基础知识-第五章 财务报告
  • 计算机算术6-SRT2除法
  • Linux817 shell:until,nfs,random
  • TypeScript中的import语法详解
  • 6.Ansible自动化之-管理变量和事实
  • 关于第一次接触Linux TCP/IP网络相关项目
  • 牛客周赛 Round 105
  • Java -- 坐标体系--绘图方法
  • 《详解 C++ Date 类的设计与实现:从运算符重载到功能测试》
  • Originality AI:原创度和AI内容检测工具
  • thingsboard 服务器在2核CPU、2G内存资源配置下如何调优提速,适合开发/演示
  • 飞算JavaAI赋能高吞吐服务器模拟:从0到百万级QPS的“流量洪峰”征服之旅
  • C#读取文件, IO 类属性及使用示例
  • [GLM-4.5] GLM-4.5模型 | Claude Code服务集成
  • Qt 5.5 的安装与配置(使用 VSCode编辑)
  • React 基础实战:从组件到案例全解析
  • Talk2BEV论文速读
  • 什么叫作数据处理?数据处理和数据治理是什么关系