Express
基于 Node.js 平台,快速、开放、极简的 web 开发框架。
1.特色
2.安装
1 | $ npm install express --save |
3.路由
路由是指如何定义应用的端点(URIs)以及如何响应客户端的请求。
路由是由一个 URI、HTTP 请求(GET、POST等)和若干个句柄组成,它的结构如下: app.METHOD(path, [callback…], callback), app 是 express 对象的一个实例, METHOD 是一个 HTTP 请求方法, path 是服务器上的路径, callback 是当路由匹配时要执行的函数。
下面是一个基本的路由示例:
1 | var express = require('express'); |
路由路径和请求方法一起定义了请求的端点,它可以是字符串、字符串模式或者正则表达式。
1 | // 匹配根路径的请求 |
使用字符串模式的路由路径示例:
1 | // 匹配 acd 和 abcd |
使用正则表达式的路由路径示例:
1 | // 匹配任何路径中含有 a 的路径: |
可以为请求处理提供多个回调函数,其行为类似 中间件。唯一的区别是这些回调函数有可能调用 next(‘route’) 方法而略过其他路由回调函数。可以利用该机制为路由定义前提条件,如果在现有路径上继续执行没有意义,则可将控制权交给剩下的路径。
1 | app.get('/example/a', function (req, res) { |
使用多个回调函数处理路由(记得指定 next 对象):
1 | app.get('/example/b', function (req, res, next) { |
使用回调函数数组处理路由:
1 | var cb0 = function (req, res, next) { |
混合使用函数和函数数组处理路由:
1 | var cb0 = function (req, res, next) { |
4.中间件
Express 是一个自身功能极简,完全是由路由和中间件构成一个的 web 开发框架:从本质上来说,一个 Express 应用就是在调用各种中间件。
中间件(Middleware) 是一个函数,它可以访问请求对象(request object (req)), 响应对象(response object (res)), 和 web 应用中处于请求-响应循环流程中的中间件,一般被命名为 next 的变量。
中间件的功能包括:
- 执行任何代码。
- 修改请求和响应对象。
- 终结请求-响应循环。
- 调用堆栈中的下一个中间件。
如果当前中间件没有终结请求-响应循环,则必须调用 next() 方法将控制权交给下一个中间件,否则请求就会挂起。
Express 应用可使用如下几种中间件:
- 应用级中间件
- 路由级中间件
- 错误处理中间件
- 内置中间件
- 第三方中间件
使用可选则挂载路径,可在应用级别或路由级别装载中间件。另外,你还可以同时装在一系列中间件函数,从而在一个挂载点上创建一个子中间件栈。
(1)应用级中间件
应用级中间件绑定到 app 对象 使用 app.use() 和 app.METHOD(), 其中, METHOD 是需要处理的 HTTP 请求的方法,例如 GET, PUT, POST 等等,全部小写。例如:
1 | var app = express() |
(2)路由级中间件
路由级中间件和应用级中间件一样,只是它绑定的对象为 express.Router()。
1 | var router = express.Router() |
1 | var app = express() |
(3)错误处理中间件
错误处理中间件和其他中间件定义类似,只是要使用 4 个参数,而不是 3 个,其签名如下: (err, req, res, next)。
1 | app.use(function(err, req, res, next) { |
(4)内置的中间件
express.static 是 Express 唯一内置的中间件。它基于 serve-static,负责在 Express 应用中提托管静态资源。每个应用可有多个静态目录。
1 | app.use(express.static('public')) |
(5)第三方中间件
安装所需功能的 node 模块,并在应用中加载,可以在应用级加载,也可以在路由级加载。
下面的例子安装并加载了一个解析 cookie 的中间件: cookie-parser
1 | $ npm install cookie-parser |
1 | var express = require('express') |
5. 获取请求参数
get
1 | req.query |
post
1 | app.use(express.urlencoded({extended:false})) |
6.利用 Express 托管静态文件
通过 Express 内置的 express.static 可以方便地托管静态文件,例如图片、CSS、JavaScript 文件等。
将静态资源文件所在的目录作为参数传递给 express.static 中间件就可以提供静态资源文件的访问了。例如,假设在 public 目录放置了图片、CSS 和 JavaScript 文件,你就可以:
1 | app.use(express.static('public')) |
现在,public 目录下面的文件就可以访问了。
1 | http://localhost:3000/images/kitten.jpg |
所有文件的路径都是相对于存放目录的,因此,存放静态文件的目录名不会出现在 URL 中。
如果你的静态资源存放在多个目录下面,你可以多次调用 express.static 中间件:
1 | app.use(express.static('public')) |
访问静态资源文件时,express.static 中间件会根据目录添加的顺序查找所需的文件。
如果你希望所有通过 express.static 访问的文件都存放在一个“虚拟(virtual)”目录(即目录根本不存在)下面,可以通过为静态资源目录指定一个挂载路径的方式来实现,如下所示:
1 | app.use('/static', express.static('public')) |
现在,你就可以通过带有 “/static” 前缀的地址来访问 public 目录下面的文件了。
1 | http://localhost:3000/static/images/kitten.jpg |
7.服务端渲染(模板引擎)
1 | npm i ejs |
需要在应用中进行如下设置才能让 Express 渲染模板文件:
- views, 放模板文件的目录,比如: app.set(‘views’, ‘./views’)
- view engine, 模板引擎,比如: app.set(‘view engine’, ‘ejs’)
- 标题: Express
- 作者: Voun
- 创建于 : 2023-08-04 16:02:00
- 更新于 : 2024-08-13 05:34:28
- 链接: http://www.voun.top/2023/08/04/02Express/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。