4

【笔记】Express学习笔记

 1 year ago
source link: https://en.loli.fj.cn/2022/12/12/Express%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Express
Fast, unopinionated, minimalist web framework for Node.js(官网

使用Express快速创建Web服务器或API接口服务器

npm install express

创建服务器

// 引入模块
const express = require("express");
// 创建服务器
const app = express();

// 监听GET请求
app.get("/", function(req, resp) {
// 响应数据
resp.send("ok");
});

// 监听POST请求
app.post("/", function(req, resp) {
// 响应数据
resp.send("ok");
});

// 启动服务器
app.listen(80, function() {
console.log("express server run at http://127.0.0.1");
});

处理GET请求参数

  • 默认情况下req.query是一个空对象
app.get("/", function(req, resp) {
console.log(req.query);
});

处理RESTful风格的GET请求参数

  • 通过req.params来接收RESTful风格的请求参数
app.get("/:key1/:key2", function(req, resp) {
console.log(req.params);
});
  • 如果发送的请求是http://127.0.0.1/value1/value2,通过req.params得到的响应如下
{
"key1": "value1",
"key2": "value2"
}

创建静态资源服务器

  • 指定一个目录,将这个目录作为静态资源目录,所有静态资源的请求可以直接被访问
  • 如果先后指定了多个相同层级的相同静态资源,此时先托管的静态资源路径会生效,后托管的静态资源路径不会生效

./public:静态资源目录名

app.use(express.static("./public"));
+ public
- index.html
  • 此时访问http://127.0.0.1/index.html时就会从静态资源中获取了

指定访问路径前缀

  • 通常情况下访问前缀名与静态资源目录名相同

/public:访问路径前缀

app.use("/public", express.static("./public"));
  • 此时访问http://127.0.0.1/public/index.html时就会从静态资源中获取了
  • 将请求类型和请求路径映射到指定处理函数,这种映射就是Express中的路由
  • 路由有优先级,会从上到下依次匹配,先被匹配成功的路由将会先执行处理函数

挂载到app对象

const express = require("express");
const app = express();

// 挂载路由
app.get("/", function(req, resp) {
// 响应数据
resp.send("ok");
});
app.post("/", function(req, resp) {
// 响应数据
resp.send("ok");
});

app.listen(80, function() {
console.log("express server run at http://127.0.0.1");
});

模块化路由

  • 为了方便模块化管理,不建议直接将路由挂载到app对象,而是将路由抽离为单独的模块
  1. 创建路由模块对应的JS文件
  2. 调用express.Router()函数创建路由对象
  3. 由路由对象挂载具体路由
  4. 使用module.exports向外共享路由对象
  5. 使用app.use()注册路由模块

router.js

const express = require("express");
const router = express.Router();

router.get("/", function() {
resp.send("ok");
});
router.post("/", function() {
resp.send("ok");
});

module.exports = router;

index.js

const express = require("express");
const app = express();

// 导入路由模块
const router = require("./router");
// 注册路由模块
app.use(router);

app.listen(80, function() {
console.log("express server run at http://127.0.0.1");
});
  • 使用app.use()来注册全局中间件

指定访问路径前缀

/router:访问路径前缀

app.use("/router", router);

中间件(Middleware)

  • 中间件指业务流程的中间处理环节
  • Express中的中间件本质上就是一个特殊的处理函数
  • 中间件处理函数的形参必须包含next,同时函数体中结尾必须包含next()函数
  • 中间件处理函数在做完处理后,会交给下一个中间件或路由进行进一步处理

全局生效中间件

  • 当客户端发起任何请求,到达服务器时,都会触发全局生效中间件
  • 通过app.use()可以设置一个中间件处理函数作为全局生效中间件
const express = require("express");
const app = express();

// 定义一个中间件函数
const func = function (req, resp, next) {

...

next();
}
// 将中间件函数设置为全局生效
app.use(func);

// 挂载路由
app.get("/", function(req, resp) {
// 响应数据
resp.send("ok");
});
app.post("/", function(req, resp) {
// 响应数据
resp.send("ok");
});

app.listen(80, function() {
console.log("express server run at http://127.0.0.1");
});

哔哩哔哩——黑马程序员


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK