Nodejs封装类似express框架的路由实例详解
作者:loaderman 时间:2024-05-11 10:17:43
代码如下
var http=require('http');
var ejs=require('ejs');
var app=require('./model/express-route.js');
console.log(app);
http.createServer(app).listen(3000);
app.get('/',function(req,res){
var msg='这是数据库的数据'
ejs.renderFile('views/index.ejs',{msg:msg},function(err,data){
res.send(data);
})
})
//登录页面
app.get('/login',function(req,res){
console.log('login');
ejs.renderFile('views/form.ejs',{},function(err,data){
res.send(data);
})
})
//执行登录
app.post('/dologin',function(req,res){
console.log(req.body); /*获取post传过来的数据*/
res.send("<script>alert('登录成功');history.back();</script>")
})
app.get('/register',function(req,res){
console.log('register');
res.send('register');
})
app.get('/news',function(req,res){
console.log('register');
res.send('新闻数据');
})
express-route.js
var url=require('url');
//封装方法改变res 绑定res.send()
function changeRes(res){
res.send=function(data){
res.writeHead(200,{"Content-Type":"text/html;charset='utf-8'"});
res.end(data);
}
}
//暴露的模块
var Server=function(){
var G=this; /*全局变量*/
//处理get和post请求
this._get={};
this._post={};
var app=function(req,res){
changeRes(res);
//获取路由
var pathname=url.parse(req.url).pathname;
if(!pathname.endsWith('/')){
pathname=pathname+'/';
}
//获取请求的方式 get post
var method=req.method.toLowerCase();
if(G['_'+method][pathname]){
if(method=='post'){ /*执行post请求*/
var postStr='';
req.on('data',function(chunk){
postStr+=chunk;
})
req.on('end',function(err,chunk) {
req.body=postStr; /*表示拿到post的值*/
//G._post['dologin'](req,res)
G['_'+method][pathname](req,res); /*执行方法*/
})
}else{ /*执行get请求*/
G['_'+method][pathname](req,res); /*执行方法*/
}
}else{
res.end('no router');
}
}
app.get=function(string,callback){
if(!string.endsWith('/')){
string=string+'/';
}
if(!string.startsWith('/')){
string='/'+string;
}
// /login/
G._get[string]=callback;
}
app.post=function(string,callback){
if(!string.endsWith('/')){
string=string+'/';
}
if(!string.startsWith('/')){
string='/'+string;
}
// /login/
G._post[string]=callback;
//G._post['dologin']=function(req,res){
//
//}
}
return app;
}
module.exports=Server();
以上代码很简单,大家可以测试下,如果有任何疑问和补充可以联系小编,更多内容可以查看以下相关知识点。
来源:https://www.cnblogs.com/loaderman/p/11504912.html
标签:Nodejs,express框架
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL数据库本地备份和双机相互备份
2008-05-27 12:25:00
Python内置的字符串处理函数详细整理(覆盖日常所用)
2023-10-10 22:46:36
基于Python的XSS测试工具XSStrike使用方法
2021-08-21 13:11:56
![](https://img.aspxhome.com/file/2023/1/101321_0s.jpg)
SQLServer:探讨EXEC与sp_executesql的区别详解
2024-01-29 02:53:53
![](https://img.aspxhome.com/file/2023/8/122398_0s.png)
setTimeout与setInterval的区别浅析
2024-04-22 13:25:25
向MySQL数据库的表中录入数据的实用方法
2008-12-17 16:24:00
python pandas dataframe 按列或者按行合并的方法
2022-04-15 13:25:19
详解Python中的日志模块logging
2021-11-24 21:58:34
![](https://img.aspxhome.com/file/2023/5/81545_0s.jpg)
OpenCV角点检测的实现示例
2023-06-25 06:49:10
Mysql systemctl start mysqld报错的问题解决
2024-01-26 03:41:45
![](https://img.aspxhome.com/file/2023/3/103573_0s.jpg)
如何在页面中对不同的数据进行相同的处理?
2010-06-26 12:30:00
Python中zipfile压缩文件模块的基本使用教程
2021-08-18 03:24:40
![](https://img.aspxhome.com/file/2023/8/110538_0s.png)
ASP Access实现网站计数器(访问量)
2011-04-10 10:33:00
Python实现迭代时使用索引的方法示例
2022-12-15 11:08:48
Python编程中NotImplementedError的使用方法
2023-06-08 15:40:40
在pycharm中使用pipenv创建虚拟环境和安装django的详细教程
2021-06-06 19:02:37
![](https://img.aspxhome.com/file/2023/9/102999_0s.png)
SQLite数据库管理相关命令的使用介绍
2024-01-27 12:41:00
Python调用两个机器人聊天的实战
2021-09-30 23:10:52
![](https://img.aspxhome.com/file/2023/9/124639_0s.png)
我跟iframe之间的误会
2008-03-17 13:30:00
PHP简单系统查询模块代码打包下载
2024-05-11 09:48:03