网站meta 优化建议,自动引流推广app,tp5被黑做的网站全变成首页,广州制作网站公司哪家好前言 将从如何使用脚手架工具开始#xff0c;快速搭建一个 Express 项目的基础架构。接着#xff0c;文章将详细讲解 Express 中间件的概念、分类以及如何有效地使用中间件来增强应用的功能和性能。最后#xff0c;我们将讨论如何制定合理的接口规范#xff0c;以确保 API …前言 将从如何使用脚手架工具开始快速搭建一个 Express 项目的基础架构。接着文章将详细讲解 Express 中间件的概念、分类以及如何有效地使用中间件来增强应用的功能和性能。最后我们将讨论如何制定合理的接口规范以确保 API 的一致性和可维护性。 一、下载express模版
根据创建的自定义bincli命令下载express模版代码
bincli create express-project切换express-project项目打开终端命令
cd express-project
npm install查看package.json是否安装了nodemon,没有的话重安一下
npm i nodemon运行项目
npm run dev报错原因:运行Express 应用时遇到了一个 ReferenceError具体来说是因为在 app.js 文件中使用了一个未定义的变量 router。 二、Express中间件与接口规范
1、启动项目
修改app.js
const express require(express);
const app express();const PORT process.env.PORT || 3000;function logs(req) {console.log(${req.method},${req.url},${Date.now()});
}app.get(/, (req, res) {logs(req);res.send(/home);
});
app.get(/register, (req, res) {logs(req);res.send(register);
});
app.get(/login, (req, res) {logs(req);res.send(/login);
});
app.listen(PORT, () {console.log(Server is running at http://localhost:${PORT});
});
运行项目
npm run dev打开postman客服端,send发送请求查看 2、注册中间件函数。
app.use()是 Express 应用的一个方法,这里注册的中间件将应用于所有路由。其中(req, res, next) { ... } 是一个箭头函数它接受三个参数。
req请求对象包含了请求的所有信息如请求方法、URL、请求头和请求体等。res响应对象用于发送响应给客户端。next一个函数调用它将请求传递给下一个中间件函数。如果不调用 next()请求将停止处理。
修改app.js
const express require(express);
const app express();const PORT process.env.PORT || 3000;
app.use((req, res, next) {console.log(${req.method},${req.url},${Date.now()});next();
});app.get(/, (req, res) {res.send(/home);
});
app.get(/register, (req, res) {res.send(register);
});
app.get(/login, (req, res) {res.send(/login);
});
app.listen(PORT, () {console.log(Server is running at http://localhost:${PORT});
}); 注意在 Express 应用中中间件的执行顺序非常重要因为它决定了请求处理的流程。app.use() 方法用于注册中间件函数这些函数会按照它们被注册的顺序依次执行。如果中间件没有正确放置可能会导致请求不经过预期的中间件处理从而影响应用的行为。 三、Express中间件分类
1、应用程序级别中间件
应用程序级别中间件是绑定到 Express 应用实例的中间件。它对所有路由和请求都有效。 使用场景:适用于全局的请求处理如日志记录、身份验证等。
app.use(function (req, res, next) {// 执行中间件逻辑next();
});2、路由级别中间件
路由级别中间件是绑定到特定路由的中间件。它只对特定路由的请求有效。 使用场景:适用于特定路由的请求处理如特定路径的权限检查、数据预处理等
修改app.js
app.get(/, function (req, res, next) {console.log(req.method);next();
}, function (req, res, next) {console.log(route)next();
});启动项目
npm run dev打开postman客户端发送请求 3、错误处理中间件
错误处理中间件用于捕获和处理在中间件链中发生的错误。 使用场景:通常放在所有其他中间件之后以便捕获所有未处理的错误。
app.use(function (err, req, res, next) {// 处理错误console.error(err.stack);res.status(500).send(服务器内部错误);
});4、内置中间件
Express API 参考手册
Express 内置了一些中间件函数用于处理常见的任务。
express.static用于提供静态文件服务。 express.json()用于解析 JSON 格式的请求体。 express.urlencoded()用于解析 URL 编码格式的请求体。
express.Router() 用于创建模块化的路由处理器
修改 router/index.js
const express require(express)
const router express.Router()
router.get(/, (req, res, next) {console.log(req.method);res.send(/home)
})router.get(/user, (req, res, next) {console.log(req.method);res.send(/users)
})
module.exports router修改app.js
const express require(express);
const router require(./router/index.js);
const app express();
app.use(router);const PORT process.env.PORT || 3000;
app.listen(PORT, () {console.log(Server is running at http://localhost:${PORT});
});运行项目
npm run dev打开客户端postman 添加路由的路径前缀。
app.js
const express require(express);
const router require(./router/index.js);
const app express();
// 任何以 node 开头的请求路径都会被这个路由处理器处理。
app.use(node, router);const PORT process.env.PORT || 3000;
app.listen(PORT, () {console.log(Server is running at http://localhost:${PORT});
});运行项目
npm run dev打开客户端postman
未添加node前缀显示请求错误 添加node前缀后 404 错误处理中间件当请求的路径没有匹配到任何定义的路由时这个中间件会被调用通常放在所有路由定义之后确保它是最后一个中间件。
app.use((req, res, next) {res.status(404).send(404 Not Found)
});500 错误处理中间件,用于捕获和处理在应用中发生的错误在所有其他中间件之后。
app.use((err, req, res, next) {console.log(err);res.status(500).send(Service Error)
});5、第三方中间件
第三方中间件是由社区开发的中间件提供了各种功能扩展。
const cors require(cors);
const morgan require(morgan);
app.use(cors());
app.use(morgan(combined));