路由提取

一、常规路由提取

  • main.js
const http=require("http");
const ejs=require("ejs");
const url=require("url");
let port=80;
let host="localhost";
let router=require("./router");
let app=http.createServer((req,res)=>{
   res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
   let path=url.parse(req.url).pathname;
   if(path!="/favicon.ico")
   {
       if(path=="/")
       {
           path="home";
       }
       else if(path=="/login")
       {
           path="login";
       }
       router[path](req,res);
   }
});
app.listen(port,host,()=>{
   console.log(`http://${host}:${port}`);
});
  • router.js
// 路由提取
let router={
   'home':(req,res)=>{
       res.end("首页");
   },
   'login':(req,res)=>{
       res.end("登录界面");
   }
}
module.exports=router;

二、模拟express框架来建立服务

  • main.js
// 模拟express框架来建立服务
const http=require("http");
const ejs=require("ejs");
let port=8080;
let host="localhost";
let app=require("./router");
http.createServer(app).listen(port,host);

// 注册路由,路由分为get路由和post路由
app.get("/",(req,res)=>{
   // 路由执行的匿名函数回调
   console.log(req.query);
   res.send("首页")
});
app.get("/login",(req,res)=>{
   // 路由执行的匿名函数回调
   console.log(req.query);
   res.send("登录界面");
});
app.get("/regest",(req,res)=>{
   // 路由执行的匿名函数回调
   console.log(req.query);
   ejs.renderFile("./view/regest.ejs",(err,html)=>{
       if(err)
       {
           throw err;
       }
       res.send(html);
   });
});
app.post("/doregest",(req,res)=>{
   console.log(req.body);
   res.send("注册");
});
  • router.js
// 路由注册模块
const url=require("url");
let obj={
   _get:{},
   _post:{}
};//存储所有的路由以及路由对应的回调函数
let app=function(req,res){
   // 写入send方法
   res.send=function(data){
       res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
       res.end(data);
   }
   // 解析路径
   let path=url.parse(req.url,true);
   let pathname=path.pathname;
   let method=req.method.toLowerCase();
   if(pathname=="/favicon.ico") return;
   if(obj['_'+method][pathname]){
       // 区分方式
       if(method=="get"){
           // 执行路由对应的回调函数
           let params=path.query;
           req.query=params;
           obj['_'+method][pathname](req,res);
       }
       else{
           // 数据监听
           let data="?";
           req.on("data",(info)=>{
               data+=info;
           });
           req.on("end",()=>{
               req.body=url.parse(data,true).query;
               obj['_'+method][pathname](req,res);
           })
       }
   }
   else{
       res.end("404");
   }
   
}
// 在整个app上挂载方法
app.get=function(string,callback){
   obj._get[string]=callback;
}
app.post=function(string,callback){
   obj._post[string]=callback;
}
module.exports=app;
  • regest.ejs
<body>
    <form action="doregest" method="POST">
        <ul>
            <li>用户名:<input type="text" name="username"></li>
            <li>密码:<input type="text" name="userpwd"></li>
            <li><button>注册</button></li>
        </ul>
    </form> 
</body>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南初️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值