动图.gif
    动图.gif

    1. //app.js
    2. const koa=require("koa");
    3. const app=new koa();
    4. const initProject=require("./config")
    5. initProject(app);
    6. app.listen(4000);
    1. //package.json
    2. {
    3. "name": "1116project",
    4. "version": "1.0.0",
    5. "main": "index.js",
    6. "license": "MIT",
    7. "dependencies": {
    8. "art-template": "^4.13.2",
    9. "koa": "^2.13.4",
    10. "koa-art-template": "^1.1.1",
    11. "koa-body": "^4.2.0",
    12. "koa-router": "^10.1.1",
    13. "koa-static": "^5.0.0",
    14. "mongodb": "^4.1.4"
    15. }
    16. }
    1. //routers-index.js
    2. const router=require("koa-router")();
    3. const main=require("../models")
    4. router.get("/login", async ctx=>{
    5. await ctx.render("login")
    6. })
    7. router.get("/register", async ctx=>{
    8. await ctx.render("register")
    9. })
    10. router.get("/home", async ctx=>{
    11. ctx.body="home"
    12. })
    13. router.post("/doRegister",async ctx=>{
    14. var {username,pwd}=ctx.request.body;
    15. var userCollection=await main("user");
    16. /* 对user查询,没有username才添加 */
    17. var res=await userCollection.find({username}).toArray();
    18. /* 1.用户名存在,停留在注册页面 */
    19. if(res.length){
    20. ctx.body="<script>alert('用户名已存在');location.href='/register'</script>"
    21. }else{
    22. /* 2. 用户名不存在,注册-进入首页 */
    23. var res=await userCollection.insertOne({username,pwd});
    24. ctx.body="<script>alert('注册成功');location.href='/home'</script>"
    25. }
    26. })
    27. router.post("/doLogin",async ctx=>{
    28. var {username,pwd}=ctx.request.body;
    29. var userCollection=await main("user")
    30. var res=await userCollection.find({username,pwd}).toArray()
    31. /* if(res.length){
    32. await ctx.redirect("/home")
    33. }else{
    34. ctx.body="<script>alert('用户名或密码错误');location.href='/login'</script>"
    35. } */
    36. if(username=="lss"&&pwd=="123456"){
    37. ctx.cookies.set("login","success");
    38. await ctx.redirect("/home")
    39. }
    40. else{
    41. ctx.body="<script>alert('用户名或密码错误');location.href='/login'</script>"
    42. }
    43. })
    44. module.exports=router;
    //modules-index.js
    
    const { MongoClient} = require('mongodb');
    // Connection URL
    const url = 'mongodb://123.60.82.25:12021';
    const client = new MongoClient(url);
    const dbName = 'student';
    async function main(table) {
      await client.connect();
      console.log('Connected successfully to server');
      const db = client.db(dbName);
      const collection = db.collection(table);
      return collection;
    }
    module.exports = main;
    
    //config-index.js
    
    const router=require("../routers");
    const koaBody=require("koa-body");
    const static=require("koa-static");
    const render=require("koa-art-template")
    const path=require("path");
    
    // const fs=require("fs")
    function initProject(app){
      app.use(async (ctx,next)=>{
        if(ctx.path=="/login"||ctx.path=="/doLogin"){
          await next();
        }else{
          /* 如果cookies存在代码正常执行,如果不存在需要停在/login */
          if(ctx.cookies.get("login")){
            await next();
          }else{
            await ctx.redirect("/login")
          }
        }
      })
        app.use(koaBody());
        app.use(router.routes());
        var res=path.join(process.cwd(),'static')
        // console.log(res);
        app.use(static(path.join(process.cwd(),"static")));
        render(app, {
            root: path.join(process.cwd(), 'pages'),
            extname: '.html',
            debug: process.env.NODE_ENV !== 'production'
          });
    }
    module.exports=initProject;
    
    //pagse-login.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">
        <title>Document</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"/>
    </head>
    <body>
        <div class="container">
            <form action="/doLogin" method="POST" role="form">
                <legend>登录页面</legend>
                <div class="form-group">
                    <label for="">姓名</label>
                    <input type="text" class="form-control" name="username" placeholder="请输入名字">
                </div>
                <div class="form-group">
                    <label for="">密码</label>
                    <input type="password" class="form-control" name="pwd" placeholder="请输入密码">
                </div>       
                <button type="submit" class="btn btn-success">登录</button>
               <a href="/register" class="btn btn-warning">快速注册</a>
            </form>
        </div>
    </body>
    </html>
    
    //pagse-register.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">
        <title>Document</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"/>
    </head>
    <body>
        <div class="container">
            <form action="/doRegister" method="POST" role="form">
                <legend>注册页面</legend>
                <div class="form-group">
                    <label for="">姓名</label>
                    <input type="text" class="form-control" name="username" placeholder="请输入名字">
                </div>
                <div class="form-group">
                    <label for="">密码</label>
                    <input type="password" class="form-control" name="pwd" placeholder="请输入密码">
                </div>
                <button type="submit" class="btn btn-danger">注册</button>   
            </form>   
        </div>
    </body>
    </html>