掌握 Express.js:深入探讨
Express 是 Node.js 中极其常用的 Web 服务器应用程序框架。本质上,框架是一种遵循特定规则的代码结构,具有两个关键特征:
- 封装了API,让开发者能够更加专注于业务代码的编写。
- 已建立流程和标准规范。
Express框架的核心特性如下:
- 它可以配置中间件来响应各种HTTP请求。
- 它定义了一个路由表,用于执行不同类型的HTTP请求操作。
- 支持向模板传递参数,实现HTML页面的动态渲染。
本文将通过实现一个简单的 LikeExpress 类来分析 Express 如何实现中间件注册、下一个机制以及路由处理。
快速分析
我们首先通过两个 Express 代码示例来探索它提供的功能:
Express 官网 Hello World 示例
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
入口文件app.js分析
以下是express-generator脚手架生成的Express项目的入口文件app.js的代码:
// Handle errors caused by unmatched routes const createError = require('http-errors'); const express = require('express'); const path = require('path'); const indexRouter = require('./routes/index'); const usersRouter = require('./routes/users'); // `app` is an Express instance const app = express(); // View engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // Parse JSON format data in post requests and add the `body` field to the `req` object app.use(express.json()); // Parse the urlencoded format data in post requests and add the `body` field to the `req` object app.use(express.urlencoded({ extended: false })); // Static file handling app.use(express.static(path.join(__dirname, 'public'))); // Register top-level routes app.use('/', indexRouter); app.use('/users', usersRouter); // Catch 404 errors and forward them to the error handler app.use((req, res, next) => { next(createError(404)); }); // Error handling app.use((err, req, res, next) => { // Set local variables to display error messages in the development environment res.locals.message = err.message; // Decide whether to display the full error according to the environment variable. Display in development, hide in production. res.locals.error = req.app.get('env') === 'development'? err : {}; // Render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app;
从上面两段代码可以看出,Express实例应用程序主要有三个核心方法:
-
app.use([path,]callback[,callback...]):用于注册中间件。当请求路径符合设定的规则时,就会执行相应的中间件函数。
- path:指定调用中间件函数的路径。
- 回调:回调函数可以采用多种形式。它可以是单个中间件函数、一系列用逗号分隔的中间件函数、中间件函数数组或以上所有函数的组合。
- app.get() 和 app.post():这些方法与 use() 类似,也是用于注册中间件。但是,它们绑定到 HTTP 请求方法。只有使用对应的HTTP请求方法才会触发相关中间件的注册。
- app.listen():负责创建一个httpServer并传递server.listen()所需的参数。
代码实现
通过对Express代码功能的分析,我们知道Express的实现重点关注三点:
- 中间件函数的注册过程。
- 中间件功能中的核心next机制。
- 路由处理,重点是路径匹配。
基于这些点,我们将在下面实现一个简单的 LikeExpress 类。
1. 类的基本结构
首先明确该类需要实现的主要方法:
- use():实现通用中间件注册。
- get() 和 post():实现HTTP请求相关的中间件注册。
- Listen():本质上就是httpServer的listen()函数。在该类的listen()函数中,创建了一个httpServer,传入参数,监听请求,回调函数(req, res) => {} 已执行。
回顾原生Node httpServer的使用:
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
相应地,LikeExpress类的基本结构如下:
// Handle errors caused by unmatched routes const createError = require('http-errors'); const express = require('express'); const path = require('path'); const indexRouter = require('./routes/index'); const usersRouter = require('./routes/users'); // `app` is an Express instance const app = express(); // View engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // Parse JSON format data in post requests and add the `body` field to the `req` object app.use(express.json()); // Parse the urlencoded format data in post requests and add the `body` field to the `req` object app.use(express.urlencoded({ extended: false })); // Static file handling app.use(express.static(path.join(__dirname, 'public'))); // Register top-level routes app.use('/', indexRouter); app.use('/users', usersRouter); // Catch 404 errors and forward them to the error handler app.use((req, res, next) => { next(createError(404)); }); // Error handling app.use((err, req, res, next) => { // Set local variables to display error messages in the development environment res.locals.message = err.message; // Decide whether to display the full error according to the environment variable. Display in development, hide in production. res.locals.error = req.app.get('env') === 'development'? err : {}; // Render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app;
2. 中间件注册
从 app.use([path,]callback[,callback...]) 中,我们可以看到中间件可以是函数数组,也可以是单个函数。为了简化实现,我们将中间件统一处理为函数数组。 LikeExpress类中use()、get()、post()这三个方法都可以实现中间件注册。只是由于请求方式不同,触发的中间件有所不同。所以我们考虑:
- 抽象出一个通用的中间件注册函数。
- 为这三个方法创建中间件函数数组,用于存储不同请求对应的中间件。由于use()是所有请求的通用中间件注册方法,因此存储use()中间件的数组是get()和post()数组的并集。
中间件队列数组
中间件数组需要放置在公共区域,以便于类中的方法访问。所以,我们把中间件数组放在constructor()构造函数中。
const http = require("http"); const server = http.createServer((req, res) => { res.end("hello"); }); server.listen(3003, "127.0.0.1", () => { console.log("node service started successfully"); });
中间件注册功能
中间件注册是指将中间件存储在对应的中间件数组中。中间件注册函数需要解析传入的参数。第一个参数可能是路由,也可能是中间件,所以需要先判断是否是路由。如果是,则原样输出;否则默认为根路由,然后将剩余的中间件参数转为数组。
const http = require('http'); class LikeExpress { constructor() {} use() {} get() {} post() {} // httpServer callback function callback() { return (req, res) => { res.json = function (data) { res.setHeader('content-type', 'application/json'); res.end(JSON.stringify(data)); }; }; } listen(...args) { const server = http.createServer(this.callback()); server.listen(...args); } } module.exports = () => { return new LikeExpress(); };
use()、get() 和 post() 的实现
通过通用的中间件注册函数register(),很容易实现use()、get()、post(),只需将中间件存储在对应的数组中即可。
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
3. 路由匹配处理
当注册函数的第一个参数是路由时,只有当请求路径匹配该路由或者是其子路由时,才会触发相应的中间件函数。所以,我们需要一个路由匹配函数,根据请求方法和请求路径提取匹配路由的中间件数组,供后续的callback()函数执行:
// Handle errors caused by unmatched routes const createError = require('http-errors'); const express = require('express'); const path = require('path'); const indexRouter = require('./routes/index'); const usersRouter = require('./routes/users'); // `app` is an Express instance const app = express(); // View engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // Parse JSON format data in post requests and add the `body` field to the `req` object app.use(express.json()); // Parse the urlencoded format data in post requests and add the `body` field to the `req` object app.use(express.urlencoded({ extended: false })); // Static file handling app.use(express.static(path.join(__dirname, 'public'))); // Register top-level routes app.use('/', indexRouter); app.use('/users', usersRouter); // Catch 404 errors and forward them to the error handler app.use((req, res, next) => { next(createError(404)); }); // Error handling app.use((err, req, res, next) => { // Set local variables to display error messages in the development environment res.locals.message = err.message; // Decide whether to display the full error according to the environment variable. Display in development, hide in production. res.locals.error = req.app.get('env') === 'development'? err : {}; // Render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app;
然后,在httpServer的回调函数callback()中,提取出需要执行的中间件:
const http = require("http"); const server = http.createServer((req, res) => { res.end("hello"); }); server.listen(3003, "127.0.0.1", () => { console.log("node service started successfully"); });
四、下一个机制的实施
Express中间件函数的参数为req、res、next,其中next是一个函数。只有调用它,中间件函数才能按顺序执行,类似于ES6 Generator中的next()。在我们的实现中,我们需要编写一个具有以下要求的 next() 函数:
- 每次从中间件队列数组中按顺序提取一个中间件。
- 将 next() 函数传递到提取的中间件中。由于中间件数组是公共的,因此每次执行next()时,都会取出数组中的第一个中间件函数执行,从而达到中间件顺序执行的效果。
const http = require('http'); class LikeExpress { constructor() {} use() {} get() {} post() {} // httpServer callback function callback() { return (req, res) => { res.json = function (data) { res.setHeader('content-type', 'application/json'); res.end(JSON.stringify(data)); }; }; } listen(...args) { const server = http.createServer(this.callback()); server.listen(...args); } } module.exports = () => { return new LikeExpress(); };
快捷代码
constructor() { // List of stored middleware this.routes = { all: [], // General middleware get: [], // Middleware for get requests post: [], // Middleware for post requests }; }
Leapcell:用于 Web 托管、异步任务和 Redis 的下一代无服务器平台
最后给大家介绍一个非常适合部署Express的平台:Leapcell。
Leapcell 是一个无服务器平台,具有以下特征:
1. 多语言支持
- 使用 JavaScript、Python、Go 或 Rust 进行开发。
2.免费部署无限个项目
- 只需支付使用费用——无请求,不收费。
3. 无与伦比的成本效益
- 即用即付,无闲置费用。
- 示例:25 美元支持 694 万个请求,平均响应时间为 60 毫秒。
4.简化的开发者体验
- 直观的用户界面,轻松设置。
- 完全自动化的 CI/CD 管道和 GitOps 集成。
- 实时指标和日志记录以获取可行的见解。
5. 轻松的可扩展性和高性能
- 自动扩展以轻松处理高并发。
- 零运营开销——只需专注于构建。
在文档中探索更多内容!
Leapcell Twitter:https://x.com/LeapcellHQ
以上是掌握 Express.js:深入探讨的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

Python和JavaScript在开发环境上的选择都很重要。1)Python的开发环境包括PyCharm、JupyterNotebook和Anaconda,适合数据科学和快速原型开发。2)JavaScript的开发环境包括Node.js、VSCode和Webpack,适用于前端和后端开发。根据项目需求选择合适的工具可以提高开发效率和项目成功率。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

JavaScript在网站、移动应用、桌面应用和服务器端编程中均有广泛应用。1)在网站开发中,JavaScript与HTML、CSS一起操作DOM,实现动态效果,并支持如jQuery、React等框架。2)通过ReactNative和Ionic,JavaScript用于开发跨平台移动应用。3)Electron框架使JavaScript能构建桌面应用。4)Node.js让JavaScript在服务器端运行,支持高并发请求。
