nodejs业务分层如何写后端接口

这里展示的是在node +express 项目中的操作 ,数据库使用的是MongoDB,前期关于express和MongoDB的文章可访问:
Nodejs后端+express框架


  • server后端接口操作:通过路由匹配------>调用对应的 Controller------>进行 Service调用------>操作模型Model

    这里以 product 为例

  • (1)先创建模型 ProductModel.js

  • (2)路由 ProductRouter.js

  • (3)ProductController.js

  • (4)ProductService.js 利用模型映射数据库,创建新的字段

  • (5)把路由配置到整个项目初始的地方 app.js

javascript 复制代码
const ProductRouter = require('./routes/admin/ProductRouter');
app.use(ProductRouter)

查看数据库,创建了product,并添加信息成功

相关推荐
qq_2290580139 分钟前
Volta的下载、安装使用教程
node.js
zh_xuan2 小时前
node.js搭建http服务
node.js
zhensherlock4 小时前
Protocol Launcher 系列:Working Copy 文件操作与高级命令详解
javascript·git·typescript·node.js·自动化·github·js
freewlt19 小时前
VS Code 扩展开发:集成 GitHub Copilot 的完整指南
vscode·node.js
技术程序猿华锋20 小时前
OpenAI GPT Image 2 教程:API Key 获取、参数说明与 Python/Node.js 示例
python·gpt·node.js·ai绘画
米丘1 天前
vue3.x 编译 script setup 编译过程
vue.js·node.js·babel
网络点点滴1 天前
Node.js从URL中解析变量
node.js
火乐暖阳851051 天前
vue3+node.js:一个基础入门的全栈CURD模块
node.js
zhensherlock1 天前
Protocol Launcher 系列:Working Copy 提交与同步全攻略
javascript·git·typescript·node.js·自动化·github·js