# NodeJS_图书管理系统 **Repository Path**: yaclty2/svn_test ## Basic Information - **Project Name**: NodeJS_图书管理系统 - **Description**: 测试SVN - **Primary Language**: NodeJS - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-04-09 - **Last Updated**: 2023-06-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 测试SVN #### 介绍 NodeJs的仓库 #### 2021-4-21 ### 回顾 内连接: 查询关联表相等的数据 外连接: 查询主表全部的数据及次表与主表相等的数据 ```mysql select * from 表名1 inner join 表名2 on 表1.列 = 表2.列 -- 内连接 left join 表名2 on 表1.列 = 表2.列 -- 左连接: left前面的是主表 right join 表名2 on 表1.列 = 表2.列 -- 左连接: right前面的是次表 ``` ### WEB 服务器对象 request:请求对象 response:相应对象 cookies:储存在用户本地终端上的数据 **实现静态服务器** ```javascript // 导入模块 const fs = require('fs'), http = require('http'); // 创建服务器 const server = http.createServer((req,res)=>{ let filePath = req.url.substr(1); // 处理请求或响应 console.log(filePath) // 如果没有任何地址表示请求首页 if(filePath.length == 0) filePath = 'index.html'; // 判断请求的文件是否存在 if(!fs.existsSync(filePath)){ res.writeHead(404); // 请求资源不存在啊 res.end('404 not found'); return; } // 获取请求资源的后缀 const ext = filePath.substr( filePath.lastIndexOf('.') + 1 ).toLowerCase(); // 根据后缀 获取对应的文件类型 let type = 'text/html'; if(ext == 'css') type = 'text/css'; else if(ext == 'js') type = 'application/javascript'; res.writeHead(200,{ 'Content-Type':type }) res.end( fs.readFileSync(filePath).toString("utf-8") ); }); // 监听端口 server.listen(3000,()=>console.log('监听到3000端口, http://localhost:3000')); ``` ##### 模板引擎的使用 - EJS 1.安装ejs模块 ```shell npm install --save ejs ``` 2.引用并使用 ```javascript // 引入模块 const ejs = require('ejs'); // 使用并编译字符串 -- 同步 const str = ejs.render(`

<%= user.name %>

`,{ user:{ name:'张三', age:18 } }); console.log(str); // 给出模板文件并编译 -- 异步 ejs.renderFile('test.ejs',{ user:{ name:'克里斯', age:18 } },function(err,str1){ console.log(str1); }); ``` ```javascript var connection = mysql.createConnection({ host : 'example.org', user : 'bob', password : 'secret' }); var pool = mysql.createPool({ connectionLimit : 10, host : 'example.org', user : 'bob', password : 'secret', database : 'my_db' }); ``` 项目结构 ├─src --- 放置模块 db.js books.js users.js history.js ├── db.js ├── books.js ├── users.js ├── history.js ├── staticfile.js ├── web.js ├─templates --- 放置所有的模板文件 ├─assets --- 放置所有的静态资源文件 ├── css ├── js ├─server.js -- 程序入口文件 └─test -- 测试代码 ### 项目架构 为了统一操作,所有模块都是暴露process方法,架构图如下: ![架构图](jiagou.png) ```javascript // 入口 createServer(function(req,res){ // 当有请求进来时的回调 // const url = ''; // 获取到请求地址 // 判断是否时静态文件 if(是静态文件) 静态模块.process(req,res) else 前置处理模块.process(req,res) }) // 静态模块 function process(req,res){ fs.readxxxx res.endxxxx } // 前置处理模块 function bindRender(req,res){ // 将渲染模板的方法绑定到req对象 req.renderTemplate = function(模板路径,参数){} } function parseRequestParam(req,res){ // 处理请求参数的 // 判断请求是get还是post req.params = {} // 讲请求的参数绑定到req上 } function process(req,res){ bindRender(req,res) parseRequestParam(req,res) } // 其他的处理模块(图书、历史、用户) function xxx(){ const q = res.params // 获取参数 res.renderTemplate(模板路径,参数) } function process(req,res){ // 根据不同的url 调用不同处理方法 if(url == "/xxx"){ xxx(req,res); } } ```