当前位置:网站首页>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=routerController:
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
边栏推荐
- CPDA|运营人如何从负基础学会数据分析(SQL)
- 【C语言】详解栈和队列(定义、销毁、数据的操作)
- Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
- SDC简介
- PHP技能评测
- 直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
- HOG feature study notes
- The difference between a process in user mode and kernel mode [exclusive analysis]
- [parameters of PyQT5 binding functions]
- [ROS](10)ROS通信 —— 服务(Service)通信
猜你喜欢

Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?

.Net C# 控制台 使用 Win32 API 创建一个窗口

没有对象的程序员如何过七夕

C language diary 9 3 kinds of statements of if

如何基于OpenVINO POT工具简单实现对模型的量化压缩

树表的查找

LeetCode使用最小花费爬楼梯----dp问题

多线程(2)

Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail

常见的硬件延迟
随机推荐
mysql树状结构查询问题
EBS uses virtual columns and hint hints to optimize sql case
如何看待自己的羞愧感
线性表的查找
散列表的查找(哈希表)
《.NET物联网从零开始》系列
如何模拟后台API调用场景,很细!
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
std::string::find 返回值的坑
Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
Xunrui cms website cannot be displayed normally after relocation and server change
C language diary 9 3 kinds of statements of if
LeetCode uses the minimum cost to climb the stairs----dp problem
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
CMS website construction process
LPQ (local phase quantization) study notes
Greenplum Database Fault Analysis - Why Does gpstart -a Return Failure After Version Upgrade?
释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
C学生管理系统 据学号查找学生节点