Node.js -Express 路由配置

3 篇文章 2 订阅
//方法一:router.js
var fs = require('fs');
var express = require('express');
console.log('Router had been excuted.');
//路由页面,用来存放各种各样的路由
module.exports = function(app){
   //主页面的views目录要和app.js目录同级
    app.get('/',function(request,response){
    //从文件中读取数据,(因为读取文件时,默认读取格式是16进制)第二个参数是文件传输格式,也可以选择用函数 data.toString()进行转换;
    fs.readFile('./database.json',function(error,data){
       //若读取失败
       if(error){
           return status(500).send('Server error'); //500状态码,服务端出错状态码
       }else{
          response.render('index.html',{
              Students: JSON.parse(data).students// 用parse将JSON数据转为对象(data 是一个对象),再选择students对象
          });   
       }
    });
    });
    app.get('/hello',function(request,response){
        response.render('hello.html');
    });
    app.get('/xxx',function(request,response){
        response.render('xxx.html');
    });
}

//方法二: router.js
var fs = require('fs');
var express = require('express');
//创建路由器,Express框架自带
var router = express.Router();
//路由容器,用来存放各种各样的路由

   //主页面的views目录要和app.js目录同级
    router.get('/',function(request,response){
    //从文件中读取数据,(因为读取文件时,默认读取格式是16进制)第二个参数是文件传输格式,也可以选择用函数 data.toString()进行转换;
    fs.readFile('./database.json',function(error,data){
       //若读取失败
       if(error){
           return status(500).send('Server error'); //500状态码,服务端出错状态码
       }else{
          response.render('index.html',{
              Students: JSON.parse(data).students// 用parse将JSON数据转为对象(data 是一个对象),再选择students对象
          });   
       }
    });
    });
    router.get('/hello',function(request,response){
        response.render('hello.html');
    });
    router.get('/xxx',function(request,response){
        response.render('xxx.html');
    });

    //导出路由容器
    module.exports  = router ;





//app.js
var express = require('express');
var fs = require('fs');
var router = require('./router');
var app =  express();
//处理页面中各种链接请求,将各种静态资源统一放在一个开头相同的文件中,当服务端收到新的链接请求时,就会到相应目录中去寻找
app.use ('/node_modules',express.static('../node_modules/'));
app.use('/views/',express.static('./views/'));
/*
  配置使用 art-template  模板引擎,第一个参数表示,当渲染以  .art 结尾的文件的时候(也可以修改为其他后缀,如: .html ), 使用
  art-template  模板引擎, express-art-template  是专门用来在 Express 中把art-template 整合到Express 中,原因在于 express-art-template 依赖了 art-template
*/
app.engine('html',require('express-art-template'));
//方法一使用router函数
router(app);
//方法二:
app.use(router);

app.listen(5000,function(){
    console.log('Running server 5000.....');
})
<--index.html!-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/views/css/index.css" >
    
    <title>Index Page</title>
</head>
<body>
    <div id="MajorBody">
        
        <div id ="Header"> 
            <span class="ProjectName"> Student Admini System</span>    
            <input type="text" value="" name ="" placeholder="Search..." class="Search" id="Search" >
            <span class="direct" id="DashBoard">
                <a href="javascript:void(0)">DashBoard</a>
            </span>
            <span class="direct" id="Settings">
                <a href="javascript:void(0)">Settings</a>
            </span>
            <span class="direct" id="Profile">
                <a href="javascript:void(0)">Profile</a>
            </span>
            <span class="direct" id="Help">
                <a href="javascript:void(0)">Help</a>
            </span>
        </div>
 
        <div id="navigator">
            <ul id="Part">
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
            </ul>

            <ul id="Part" class="Two">
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
            </ul>

            <ul id="Part" class="Three">
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
                
                <li>
                    <a href="#">Overview</a>
                </li>
            </ul>
        </div>

        <div id="Board">
            <div id="ProfileBoard">

                <div class="ins">
                    <div class="image">
                        <img src="/views/img/猫咪投降.jpg" alt="">
                    </div>
                    <span class = "Name">Cat</span>
                    <span class = "introduction">Miao~Miao~</span>
                </div>

                <div class="ins">
                    <div class="image">
                        <img src="/views/img/小马嚎叫.jpg" alt="">
                    </div>
                    <span class = "Name">Fang</span>
                    <span class = "introduction">我很强,我知道</span>
                </div>

                <div class="ins">
                    <div class="image">
                        <img src="/views/img/李信.png" alt="">
                    </div>
                    <span class = "Name">Xin</span>
                    <span class = "introduction">不为不存在的天命而压低脊梁</span>
                </div>

                <div class="ins">
                    <div class="image">
                        <img src="/views/img/Yuyuan.jpg" alt="">
                    </div>
                    <span class = "Name">Yuyuan</span>
                    <span class = "introduction">今天吃啥?</span>
                </div>

            </div> 

            <div id="ControlButton">
                <button id="Add">Add Student</button>
            </div>

            <div id="InfoBoard">

                <div class="Title">
                    <div class="Sno">Sno</div>
                    <div class="SName">Name</div>
                    <div class="SAge">Age</div>
                    <div class="SGender">Gender</div>
                    <div class="SHobbies">Hobbies</div>
                </div>
                
                <ul id="Info">
                    {{ each Students}}
                    <li >
                        <div class="Sno"> {{ $value.Sno }}</div>
                        <div class="SName">{{ $value.Name }}</div>
                        <div class="SAge">{{ $value.Age }}</div>
                        <div class="SGender">{{ $value.Gender }} </div>
                        <div class="SHobbies">{{ $value.Hobbies }}</div>
                    </li>
                     {{ /each }}
                </ul>
            </div>
        </div>

    </div>
</body>
</html> 




-index.css
*{
    margin:0;
    padding: 0;
    box-sizing: border-box;
}
#MajorBody{
   display: flex;
   justify-content: flex-start;
   flex-wrap: wrap;
   width: 1520px;
   height: 1500px;

}

#Header{
   display: flex; 
   justify-content: flex-start;
   align-items: center;
   position:fixed;
   width: 1520px;
   height: 100px;
   color: white;
   background-color: rgba(10, 9, 9, 0.68);
}

.ProjectName{
   padding-left: 10px;
   font-size: 30px;
}

.Search{
   position: relative;
   width: 200px;
   height: 50px;
   margin-left: 450px;
   padding-left: 10px;
   border-radius: 15px;
   border:none;
   outline: none;
   font-size: 25px;
   color:rgba(128, 128, 128, 0.496);
}
.direct{
   display:flex;
   justify-content: flex-start;
   align-items: center;
   width: 120px;
   height: 90px;
}
.direct > a{
   display: inline-flex;
   flex-direction: column;
   justify-content: center;
   align-items: center;
   width: 110px;
   height: 80px;
   font-size: 20px;
   
   text-decoration: none;
   color:aliceblue;
   
}
.direct > a:hover{
   color: aqua;
}
.direct :nth-of-type(1){
   margin-left: 20px;
}
#navigator{
    display: flex;
    flex-direction:column;
    justify-content: flex-start;
    align-items: center;
    width: 200px;
    margin-top: 100px;
    height: 1200px;
    background-color: rgba(194, 218, 240, 0.938);
}

#Part{
   display: flex;
   flex-direction: column;
   justify-content: center;
   align-items: center;
   width: 200px;
   height: 400px;
 
}
#Part > li{
   display: flex;
   justify-content: center;
   align-items: center;
   width:199px;
   height: 70px;
   margin: 3px 5px;
   background-color: rgba(128, 185, 241, 0.442);
}
#Part >li > a{
   display:flex;
   justify-content: flex-start;
   align-items: center;
   width: 100px;
   height: 6px;
   text-decoration: none;
   color: black;
}
#Part >li:hover{
   cursor: pointer;
   background-color: rgba(255, 166, 0, 0.585);
}

#Part > li > a:hover{
   color: white;
}

.Two{
   margin-top: -80px;
}

.Three{
   margin-top: -80px;
}
#Board{
   display: flex;
   justify-content: flex-start;
   flex-wrap: wrap;
   width: 1320px;
   height: 1200px;
   margin-top: 100px;
}

#ProfileBoard{
   display: flex;
   justify-content: flex-start;
   align-items: center;
   width: 1500px;
   height: 300px;
   background-color: rgba(219, 233, 233, 0.755);
}

.ins{
   display:flex;
   justify-content: center;
   align-items:center;
   flex-wrap:wrap;
   width: 300px;
   height: 300px;
   color:rgb(81, 81, 81);
}
.ins > .image> img{
   display:flex;
   width: 150px;
   height: 150px;
   border-radius: 50%;
   
}
.ins .image{
   display:flex;
   justify-content: center;
   align-items: center;
   width: 150px;
   height: 150px;
   border-radius: 50%;
  
}

.ins .Name{
   display: flex;
   justify-content: center;
   align-items: center;
   width: 200px;
   height: 30px;
   margin-top: -20px;
}
.ins .introduction{
   display: flex;
   justify-content: center;
   align-items: center;
   width: 200px;
   height: 50px;
   margin-top: -50px;
}

div[class = "ins"]:nth-child(2){
   margin-left: 30px;
}

div[class = "ins"]:nth-child(3){
   margin-left: 30px;
}
 
div[class = "ins"]:nth-child(4){
   margin-left: 30px;
}
#ControlButton{
   display: flex;
   justify-content: flex-start;
   align-items: center;
   width: 1520px;
   height: 80px;
   background-color: rgba(219, 233, 233, 0.755);
}

#Add{
   display: flex;
   justify-content: center;
   align-items: center;
   margin-left:600px;
   width: 80px;
   height: 60px;
   border-radius: 15px;
   background-color: aqua;
   outline: none;
}
#Add:hover{
   color:white;
   cursor: pointer;
}

#InfoBoard{
   display: flex;
   flex-direction: column;
   justify-content: flex-start;
   flex-wrap:wrap;
   width: 1520px;
   height: 820px;
   background-color: rgba(127, 255, 212, 0.503);
}

.Title{
   display:flex;
   width: 1500px;
   height: 60px;
   justify-content: flex-start;
   align-items: center;
   border-bottom: 2px solid gray;
}

.Sno , .SName , .SGender , .SHobbies , .SAge{
    display:flex;
    justify-content: center;
    align-items: center;
    width: 200px;
    height: 60px;
    border: 1px solid gray;
}

.SHobbies{
   width: 520px;
}
#Info , #Info > li{
   display:flex;
   width: 1500px;
   height: 60px;
   justify-content: flex-start;
   flex-wrap: wrap;
   align-items: center;
   list-style: none;
}

服务端渲染页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值