当前位置:网站首页>nodeJs--encapsulate routing
nodeJs--encapsulate routing
2022-08-05 02:18:00 【H5_ljy】
Article table of contents
I. What is routing
Routing: Refers to different URLs to execute different branches or programs
Second, encapsulating routing
Requirements: Enter the URL at the front end and return the corresponding data at the back end. First enter the URL and then go to the statically hosted folder to find it; if not, go to our data interface, and if not, pass a string such as "404 not found".
**Static hosting:** When the user enters the URL, we can use the middleware in the backend to add a folder at the beginning of the pathname to read in the url of readFile, which is achieved, the user can only accessfiles in that folder.
For example:
var url=require("url")var http=require("http")var fs=require("fs")http.createServer((req,res)=>{let pathname=url.parse(req.url).pathnamefs.readFile(__dirname+'/public'+path,(err,data)=>{ // '/public' is the folder we want to hostres.end(data)}}).listen(8080)
Design idea: According to the needs, the MVC programming idea is used to design.
What is MVC
V stands for View View refers to the interface that the user sees and interacts with.For example, a web interface composed of html elements, or a client interface of a software.One of the benefits of MVC is that it can handle many different views for an application.No real processing happens in the view, it's just a way to output data and allow the user to manipulate it.
M stands for model model, which means that the model represents business rules.Of the three components of MVC, the model has the most processing tasks.The data returned by the model is neutral, and the model has nothing to do with the data format. Such a model can provide data for multiple views. Since the code applied to the model only needs to be written once and can be reused by multiple views, code duplication is reducedsex.
C is the controller controller means that the controller accepts the user's input and calls the model and view to complete the user's needs. The controller itself does not output anything or do any processing.It just receives the request and decides which model component to call to handle the request, and then decides which view to use to display the returned data.
Simply put: M refers to the business model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation codes of M and V, so that the same program can use different expressions.
Route source code:
Business Model:
var obj={}var fs=require("fs")var url=require("url")var mime=require("mime")var controller=require("./controller.js")var router=function(req,res){let pathname=url.parse(req.url).pathnamelet type=mime.getType(pathname)fs.readFile(router.basename+pathname,(err,data)=>{if(!err){res.setHeader("content-Type",type)res.end(data)}else{if(obj[pathname]){obj[pathname](req,res)}else{res.end("404 not found")}}})}router.basename=__dirname+'/src'router.stacte=function(path){this.basename=path}router.get=function(path,callback){obj[path]=callback}module.exports=router
Controller:
module.exports={ajax1(req,res){var obj={name:'ljy',age:22}var str=JSON.stringify(obj)res.setHeader("content-Type","text/json;charts-utf8")res.end(str)}//Write the data returned by the business function to the front end here}
view interface
var http=require("http")var router=require("./router.js")var constroller=require("./controller.js")http.createServer(router).listen(8081) //Set the server, followed by the port number for setting the listeningrouter.get("/ajax1",constroller.ajax1) //Add data interface, you need to configure the business function of the controllerrouter.stacte(__dirname+'/src') //Set static hosting files
边栏推荐
- Programmer's list of sheep counting when insomnia | Daily anecdote
- 继承关系下构造方法的访问特点
- [ROS](10)ROS通信 —— 服务(Service)通信
- Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
- 关于#sql shell#的问题,如何解决?
Utilities - 【MySQL series】- Does LIKE query start with % will make the index invalid?
- Greenplum数据库故障分析——版本升级后gpstart -a为何返回失败
- [parameters of PyQT5 binding functions]
- Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
猜你喜欢
开篇-开启全新的.NET现代应用开发体验
HOG特征学习笔记
DAY23:命令执行&代码执行漏洞
Xunrui cms website cannot be displayed normally after relocation and server change
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
C语言日记 9 if的3种语句
C language diary 9 3 kinds of statements of if
sql语句多字段多个值如何进行排序
使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
随机推荐
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
海量服务实例动态化管理
C language basics -- pointers
重新审视分布式系统:永远不会有完美的一致性方案……
LPQ(局部相位量化)学习笔记
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
使用OpenVINO实现飞桨版PGNet推理程序
js中try...catch和finally的用法
Residential water problems
RAID disk array
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
特殊矩阵的压缩存储
蚁剑高级模块开发
02 【开发服务器 资源模块】
SuperMap支持的国产环境汇总
iNFTnews | What can NFTs bring to the sports industry and fans?
Domain Driven Design - MDD
C学生管理系统 头添加学生节点
线性表的查找