Node.js基础---Express中间件

在Node.js中,Express是一个非常流行的web开发框架,它提供了许多内置的中间件,例如路由、静态文件服务、模板渲染等。

中间件是一种装配到应用程序中的函数,它可以访问请求对象(req),响应对象(res),以及应用程序的请求-响应循环的下一个中间件函数(next)。

下面是一些使用Express中间件的示例:

  1. 创建一个简单的中间件,它将在请求到达路由处理程序之前记录一些信息:



const express = require('express');
const app = express();
 
// 自定义中间件
app.use((req, res, next) => {
    console.log('Some info');
    next();
});
 
app.get('/', (req, res) => {
    res.send('Hello World!');
});
 
app.listen(3000, () => {
    console.log('Server is running on port 3000');
});
  1. 使用Express的内置中间件处理静态文件:



const express = require('express');
const app = express();
 
// 静态文件中间件
app.use(express.static('public'));
 
app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

在上述代码中,如果请求的路径对应于public目录中的文件,那么静态文件中间件将提供这些文件。

  1. 使用Express的内置中间件处理JSON解析:



const express = require('express');
const app = express();
 
// JSON解析中间件
app.use(express.json());
 
app.post('/', (req, res) => {
    console.log(req.body);
    res.send('Success');
});
 
app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

在上述代码中,JSON解析中间件将req.body属性设置为一个对象,该对象包含解析的请求体中的JSON对象。

  1. 使用Express的内置中间件处理URL编码:



const express = require('express');
const app = express();
 
// URL编码中间件
app.use(express.urlencoded({ extended: true }));
 
app.post('/', (req, res) => {
    console.log(req.body);
    res.send('Success');
});
 
app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

在上述代码中,URL编码中间件将req.body属性设置为一个对象,该对象包含解析的请求体中的URL编码的数据。

  1. 使用Express的内置中间件处理路由:



const express = require('express');
const app = express();
 
// 路由中间件
app.use('/user', (req, res, next) => {
    console.log('User middleware');
    next();
});
 
app.get('/user', (req, res) => {
    res.send('User info');
});
 
app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

在上述代码中,路由中间件仅在请求路径以/user开头时触发。

  1. 错误处理中间件,它可以捕获所有错误:



const express = require('express');
const app = express();
 
// 错误处理中间件
app.use((err, req, res, next) => {
    console.error

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日