接口API

  • 不同功能层之间的通信规则
  • 主要概念:
    • 参数 :username,password
    • 返回值,一般是json
  • 文档、使用案例
  • json模拟的数据库

这里设计登录使用get,注册使用post

实现登录与注册

代码写的很烂,基本上是能用就行
app.js

  1. const http = require("http");
  2. const querystring = require("querystring");
  3. const fs = require("fs");
  4. const users = {
  5. admin: 123456,
  6. };
  7. const loginError = {
  8. err: 1,
  9. msg: "用户名不存在",
  10. };
  11. http
  12. .createServer((req, res) => {
  13. //获取数据
  14. let path;
  15. if (req.method === "GET") {
  16. const data = new URL(`${req.url}`, "http://localhost:8080/"); // new一个URL实例
  17. // console.log(data); // 可以打印出来看看里面有什么,
  18. const username = data.searchParams.get("username"); // 通过searchParams的get可以获取到想要获取的数据
  19. const password = data.searchParams.get("password");
  20. path = data.pathname;
  21. const get = { username, password };
  22. // console.log(get);
  23. // console.log(path);
  24. complete(get);
  25. } else if (req.method === "POST") {
  26. let arr = [];
  27. path = req.url;
  28. // get = { username, password };
  29. // console.log("22222", path);
  30. req.on("data", buffer => {
  31. arr.push(buffer);
  32. });
  33. req.on("end", () => {
  34. const data = Buffer.concat(arr).toString();
  35. const post = querystring.parse(data);
  36. // console.log("@", post);
  37. complete(post);
  38. });
  39. }
  40. function complete(data) {
  41. if (path === "/login") {
  42. res.writeHead(200, {
  43. "Content-Type": "text/plain;charset=utf-8",
  44. });
  45. const { username, password } = data;
  46. // console.log(get);
  47. if (!users[username]) {
  48. res.end(JSON.stringify(loginError));
  49. } else if (users[username] != password) {
  50. // console.log(users[username]);
  51. // console.log(password);
  52. res.end(
  53. JSON.stringify({
  54. err: 1,
  55. msg: "密码错误",
  56. })
  57. );
  58. } else {
  59. res.end(
  60. JSON.stringify({
  61. err: 0,
  62. msg: "登录成功",
  63. })
  64. );
  65. }
  66. } else if (path === "/reg") {
  67. res.writeHead(200, {
  68. "Content-Type": "text/plain;charset=utf-8",
  69. });
  70. const { username, password } = data;
  71. // console.log("@", data);
  72. if (users[username]) {
  73. res.end(
  74. JSON.stringify({
  75. err: 1,
  76. msg: "账户已存在",
  77. })
  78. );
  79. } else {
  80. users[username] = password;
  81. // console.log("@", users);
  82. res.end(
  83. JSON.stringify({
  84. err: 0,
  85. msg: "注册成功",
  86. })
  87. );
  88. }
  89. } else {
  90. fs.readFile(`static/${path}`, (err, data) => {
  91. err ? res.end("404") : res.end(data);
  92. });
  93. }
  94. }
  95. })
  96. .listen(8080);

login.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Login</title>
  8. <link rel="stylesheet" type="text/css" href="./css/login.css" />
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1>Please Login</h1>
  13. <form>
  14. <div class="form-control">
  15. <input type="text" required id="username" />
  16. <label>
  17. Email
  18. <!-- <span style="transition-delay : 0ms;">E</span>
  19. <span style="transition-delay : 50ms;">m</span>
  20. <span style="transition-delay : 100ms;">a</span>
  21. <span style="transition-delay : 150ms;">i</span>
  22. <span style="transition-delay : 200ms;">l</span> -->
  23. </label>
  24. </div>
  25. <div class="form-control">
  26. <input type="password" required id="password">
  27. <label>Password</label>
  28. </div>
  29. <button class="btn" id="login">Login</button>
  30. <p class="text">
  31. Dont't have an account?<button class="btn small" id="reg">Register</button>
  32. </p>
  33. </form>
  34. </div>
  35. <script>
  36. const labels = document.querySelectorAll('.form-control label');
  37. labels.forEach(label => {
  38. label.innerHTML = label.innerText.split('')
  39. .map((letter, idx) => `<span style="transition-delay:${idx*50}ms">${letter}</span>`)
  40. .join('');
  41. })
  42. </script>
  43. <script src='./jquery.min.js'></script>
  44. <script>
  45. $('#login').click(function () {
  46. $.ajax({
  47. url: "/login",
  48. data: {
  49. username: $('#username').val(),
  50. password: $('#password').val()
  51. },
  52. dataType: "json",
  53. success(res) {
  54. if (res.err) {
  55. alert(res.msg)
  56. } else {
  57. alert("登录成功");
  58. location.href = 'admin.html'
  59. }
  60. }
  61. })
  62. })
  63. $('#reg').click(function () {
  64. $.ajax({
  65. url: "/reg",
  66. method: 'post',
  67. data: {
  68. username: $('#username').val(),
  69. password: $('#password').val()
  70. },
  71. dataType: "json",
  72. success(res) {
  73. console.log("@", res)
  74. res.err ?
  75. alert(res.msg) :
  76. alert("注册成功")
  77. }
  78. })
  79. })
  80. </script>
  81. </body>
  82. </html>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. body {
  7. background-color: steelblue;
  8. color: white;
  9. font-family: sans-serif;
  10. display: flex;
  11. justify-content: center;
  12. align-items: center;
  13. height: 100vh;
  14. }
  15. .container {
  16. background-color: rgba(0, 0, 0, 0.4);
  17. padding: 20px 40px;
  18. border-radius: 5px;
  19. }
  20. .container h1 {
  21. text-align: center;
  22. margin-bottom: 30px;
  23. }
  24. .container a {
  25. text-decoration: none;
  26. color: lightblue;
  27. }
  28. .btn {
  29. cursor: pointer;
  30. width: 100%;
  31. background-color: lightblue;
  32. padding: 15px;
  33. border: 0;
  34. font-size: 16px;
  35. font-family: inherit;
  36. }
  37. .btn:focus {
  38. outline: 10px solid pink;
  39. }
  40. .btn:active {
  41. transform: scale(0.98);
  42. }
  43. .btn.small {
  44. display:inline;
  45. width: 100px;
  46. height:40px;
  47. text-align: center;
  48. }
  49. .text {
  50. margin-top: 30px;
  51. }
  52. .form-control {
  53. position: relative;
  54. width: 300px;
  55. margin: 20px 0 40px;
  56. }
  57. .form-control input {
  58. background-color: transparent;
  59. border: 0;
  60. border-bottom: 2px solid white;
  61. display: block;
  62. width: 100%;
  63. padding: 15px 0;
  64. font-size: 18px;
  65. color: white;
  66. }
  67. .form-control input:focus {
  68. outline: 0;
  69. border-bottom-color: lightblue;
  70. }
  71. .form-control input:focus + label span {
  72. transform: translateY(-30px);
  73. color: lightblue;
  74. }
  75. .form-control input:valid {
  76. border-bottom-color: lightblue;
  77. }
  78. .form-control input:valid + label span {
  79. transform: translateY(-30px);
  80. color: lightblue;
  81. }
  82. .form-control label {
  83. position: absolute;
  84. left: 0;
  85. top: 15px;
  86. }
  87. .form-control label span {
  88. display: inline-block;
  89. font-size: 18px;
  90. transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  91. }

css不是重点,我只是把之前做的好看的login界面拿来用了\
admin.html 登录成功后的界面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Login</title>
  8. <link rel="stylesheet" type="text/css" href="./css/login.css" />
  9. </head>
  10. <body>
  11. <div class="container">
  12. <form>
  13. <p>登录成功</p>
  14. </form>
  15. </div>
  16. </body>
  17. </html>

总结

深入建议

  • koa
  • express
  • 数据库:mongodb、mysql