From c83742e0c9d642017ae97eec7c95bfd77b76b0a1 Mon Sep 17 00:00:00 2001 From: Oasis Date: Fri, 16 Jun 2023 23:49:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0postman-collection=EF=BC=8C?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 73 ++--- controllers/ImgCtrl.js | 75 +++-- controllers/loginCtrl.js | 18 +- postman_api_test.json | 44 --- ...4\200\345\217\221.postman_collection.json" | 300 ++++++++++++++++++ 5 files changed, 376 insertions(+), 134 deletions(-) delete mode 100644 postman_api_test.json create mode 100644 "\350\267\250\350\204\232\346\234\254\345\274\200\345\217\221.postman_collection.json" diff --git a/app.js b/app.js index ae371fb..c38d79c 100644 --- a/app.js +++ b/app.js @@ -1,67 +1,54 @@ -var express = require('express'); -const https = require("https"); -const fs = require("fs"); -const path = require("path"); +var express = require('express'); // 导入express模块,用于创建Web应用 +const https = require("https"); // 导入https模块,用于创建HTTPS服务器 +const fs = require("fs"); // 导入fs模块,用于读取文件 +const path = require("path"); // 导入path模块,用于处理文件路径 -// var usersRouter = require('./routes/UserGate'); -// var registerRouter = require('./routes/RegisterGate') -var bookRouter = require('./routes/BookGate'); -var borrowRouter = require('./routes/BorrowGate'); -const loginRouter = require('./routes/LoginGate') -const ImgRouter = require('./routes/ImgGate') +var bookRouter = require('./routes/BookGate'); // 导入自定义的BookGate路由模块 +var borrowRouter = require('./routes/BorrowGate'); // 导入自定义的BorrowGate路由模块 +const loginRouter = require('./routes/LoginGate'); // 导入自定义的LoginGate路由模块 +const ImgRouter = require('./routes/ImgGate'); // 导入自定义的ImgGate路由模块 -var bodyParser = require('body-parser') +var bodyParser = require('body-parser'); // 导入body-parser模块,用于解析请求体 -//获取证书 +// 获取证书 const keysPath = path.join(__dirname, "./public"); -const ca = fs.readFileSync(`${keysPath}/ca.csr`); -const cert = fs.readFileSync(`${keysPath}/ca.crt`); -const key = fs.readFileSync(`${keysPath}/ca.key`); +const ca = fs.readFileSync(`${keysPath}/ca.csr`); // 读取ca.csr文件 +const cert = fs.readFileSync(`${keysPath}/ca.crt`); // 读取ca.crt文件 +const key = fs.readFileSync(`${keysPath}/ca.key`); // 读取ca.key文件 -var app = express(); +var app = express(); // 创建express应用 -app.use(express.json()); -app.use(express.urlencoded({ - extended: false -})); -app.use(bodyParser.urlencoded({ - extended: true -})); +app.use(express.json()); // 解析JSON请求体 +app.use(express.urlencoded({ extended: false })); // 解析URL编码请求体 +app.use(bodyParser.urlencoded({ extended: true })); // 使用body-parser中间件解析URL编码请求体 -console.log("server start!") +console.log("server start!"); -//设置跨域请求 +// 设置跨域请求 app.all('*', function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild'); res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); - res.header("X-Powered-By", ' 3.2.1') + res.header("X-Powered-By", ' 3.2.1'); res.header("Content-Type", "application/json;charset=utf-8"); next(); }); -//增加helmet +// 增加helmet,用于加强Web应用的安全性 const helmet = require("helmet"); app.use(helmet()); -//启用cors +// 启用cors,用于处理跨域请求 const cors = require("cors"); app.use(cors()); -//确认接收到请求 -// app.use('/', function (req, res, next) { -// console.log('get https request') -// res.send('Hello!') -// }) +// 注册路由中间件 +app.use('/book', bookRouter); // 将BookGate路由模块挂载到/book路径下 +app.use('/borrow', borrowRouter); // 将BorrowGate路由模块挂载到/borrow路径下 +app.use('/login', loginRouter); // 将LoginGate路由模块挂载到/login路径下 +app.use('/Img', ImgRouter); // 将ImgGate路由模块挂载到/Img路径下 -// app.use('/users', usersRouter); -// app.use('/register', registerRouter) -app.use('/book', bookRouter); -app.use('/borrow', borrowRouter); -app.use('/login', loginRouter) -app.use('/Img', ImgRouter) - -//建立https服务器 +// 建立HTTPS服务器 https.createServer({ ca, cert, key }, app).listen(8443, () => { - console.log('server is on https://localhost:8443') -}); \ No newline at end of file + console.log('server is on https://localhost:8443'); +}); diff --git a/controllers/ImgCtrl.js b/controllers/ImgCtrl.js index 35042df..de2c09b 100644 --- a/controllers/ImgCtrl.js +++ b/controllers/ImgCtrl.js @@ -1,67 +1,66 @@ -const express = require('express'); -const app = express(); -const path = require('path'); -const axios = require('axios'); -const fs = require('fs'); +const express = require('express'); // 导入Express框架 +const app = express(); // 创建Express应用 +const path = require('path'); // 导入path模块,用于处理文件路径 +const axios = require('axios'); // 导入axios模块,用于发送HTTP请求 +const fs = require('fs'); // 导入fs模块,用于操作文件系统 -const { client } = require('../util/ossconfig') +const { client } = require('../util/ossconfig'); // 导入自定义的ossconfig模块中的client对象 -// 定义filepath下载地址 -// const filePath = 'D:/Word/Study/跨平台脚本/大作业/bigwork/asserts/' -const filePath = path.dirname(__dirname) + '/asserts/' -// 处理post请求,上传文件到阿里云图床 +// 定义文件下载地址的路径 +const filePath = path.dirname(__dirname) + '/asserts/'; + +// 处理POST请求,将文件上传到阿里云图床 uploadImg = async (req, res) => { console.log(`enter controller uploadImg`); try { - const { file } = req - const { filename } = file + const { file } = req; // 从请求中获取文件对象 + const { filename } = file; // 获取文件名 - // 获取文件后缀名,如'.jpg' - let index = filename.lastIndexOf('.') - let type = filename.substr(index) + // 获取文件后缀名,例如'.jpg' + let index = filename.lastIndexOf('.'); + let type = filename.substr(index); - // 随机生成16进制 - let fileName = Math.random().toString(16).slice(2) + // 随机生成一个16进制数作为文件名 + let fileName = Math.random().toString(16).slice(2); // 拼接新文件名(随机数+文件后缀名) - fileName = `${fileName}${type}` + fileName = `${fileName}${type}`; - // 上传文件,这里是上传到OSS的 upload文件夹下 - const result = await client.put('upload/' + fileName, fs.createReadStream(file.path)) + // 上传文件,使用OSS的client对象将文件流上传到'upload'文件夹下 + const result = await client.put('upload/' + fileName, fs.createReadStream(file.path)); console.log('success put'); - // 删除该文件 - // fs.unlinkSync(file.path) + if (result) { - res.status(200).send('success uploadImg') - } - else { - res.status(500).send('failed upload!') + res.status(200).send('success uploadImg'); // 如果上传成功,返回成功状态 + } else { + res.status(500).send('failed upload!'); // 如果上传失败,返回失败状态 } } catch (error) { console.log(error); - res.status(400).send('error') + res.status(400).send('error'); // 如果出现错误,返回错误状态 } -} +}; -// 处理get请求,下载相应文件到本地 +// 处理GET请求,将相应文件下载到本地 downloadImg = async (req, res) => { console.log(`enter controller downloadImg`); console.log(filePath); try { - const { fileName } = req.body + const { fileName } = req.body; // 从请求体中获取文件名 - const result = await client.get('upload/' + fileName, filePath + fileName) + // 从OSS下载文件到本地的指定路径 + const result = await client.get('upload/' + fileName, filePath + fileName); if (result) { console.log('success downloadImg'); - res.status(200).send('success downloadImg') - } - else { + res.status(200).send('success downloadImg'); // 如果下载成功,返回成功状态 + } else { console.log('failed downloadImg!'); - res.status(500).send('failed download!') + res.status(500).send('failed download!'); // 如果下载失败,返回失败状态 } } catch (error) { console.log(error); - res.status(400).send('error') + res.status(400).send('error'); // 如果出现错误,返回错误状态 } -} +}; -module.exports = { uploadImg, downloadImg } \ No newline at end of file +// 导出uploadImg和downloadImg函数作为模块的公共接口 +module.exports = { uploadImg, downloadImg }; diff --git a/controllers/loginCtrl.js b/controllers/loginCtrl.js index aedce83..654bad4 100644 --- a/controllers/loginCtrl.js +++ b/controllers/loginCtrl.js @@ -1,29 +1,29 @@ -var User = require('../models/User'); -const jwt = require('jsonwebtoken') +var User = require('../models/User'); // 导入自定义的User模块 +const jwt = require('jsonwebtoken'); // 导入jsonwebtoken模块,用于生成和验证JWT令牌 // 登录功能的控制器 login = async (req, res) => { - const { username, password } = req.body + const { username, password } = req.body; // 从请求体中获取用户名和密码 console.log(`enter controller login %${username} ${password}%`); try { - const user = await User.findOne({ + const user = await User.findOne({ // 在数据库中查找符合用户名和密码的用户 where: { name: username, pwd: password } }); - if (user) { //如果成功 + if (user) { // 如果找到了用户 const { name } = user; jwt.sign({ name }, 'sence&oassis', { algorithm: "HS256", expiresIn: "1h" }, (err, token) => { res.status(200).send(token); // 登录成功,生成JWT令牌,发送200状态码和令牌作为响应 }); - } - else { //如果失败 - res.status(404).send('Missing username or password') + } else { // 如果没有找到用户 + res.status(404).send('Missing username or password'); // 登录失败,发送404状态码和错误消息作为响应 } } catch (error) { - res.status(404).send(error.message) + res.status(404).send(error.message); // 如果出现错误,发送404状态码和错误消息作为响应 } } +// 导出login函数作为模块的公共接口 module.exports = { login }; diff --git a/postman_api_test.json b/postman_api_test.json deleted file mode 100644 index 4f8423b..0000000 --- a/postman_api_test.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "info": { - "_postman_id": "6b8230e3-8456-47ec-b24a-4cf629bc8db4", - "name": "js", - "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", - "_exporter_id": "23924843" - }, - "item": [ - { - "name": "login", - "request": { - "method": "POST", - "header": [], - "body": { - "mode": "urlencoded", - "urlencoded": [ - { - "key": "username", - "value": "admin", - "type": "text" - }, - { - "key": "password", - "value": "123456", - "type": "text" - } - ] - }, - "url": { - "raw": "https://localhost:8443/login", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "8443", - "path": [ - "login" - ] - } - }, - "response": [] - } - ] -} \ No newline at end of file diff --git "a/\350\267\250\350\204\232\346\234\254\345\274\200\345\217\221.postman_collection.json" "b/\350\267\250\350\204\232\346\234\254\345\274\200\345\217\221.postman_collection.json" new file mode 100644 index 0000000..47c66f0 --- /dev/null +++ "b/\350\267\250\350\204\232\346\234\254\345\274\200\345\217\221.postman_collection.json" @@ -0,0 +1,300 @@ +{ + "info": { + "_postman_id": "3acb92e8-5381-493a-854e-5143c7f2c121", + "name": "跨脚本开发", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "24158169" + }, + "item": [ + { + "name": "login", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "username", + "value": "admin", + "type": "text" + }, + { + "key": "password", + "value": "123456", + "type": "text" + } + ] + }, + "url": { + "raw": "https://localhost:8443/login", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "8443", + "path": [ + "login" + ] + } + }, + "response": [] + }, + { + "name": "getAllBooks", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2ODY4MTk0MzYsImV4cCI6MTY4NjgyMzAzNn0.SOR2I4IHdYLE79HCzcb1DYbFIe3rCnHGIM4IpWskvpU", + "type": "string" + } + ] + }, + "method": "GET", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [] + }, + "url": { + "raw": "https://localhost:8443/book/getAll", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "8443", + "path": [ + "book", + "getAll" + ], + "query": [ + { + "key": "", + "value": "", + "disabled": true + } + ] + } + }, + "response": [] + }, + { + "name": "findBook", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2ODY4MTk0MzYsImV4cCI6MTY4NjgyMzAzNn0.SOR2I4IHdYLE79HCzcb1DYbFIe3rCnHGIM4IpWskvpU", + "type": "string" + } + ] + }, + "method": "GET", + "header": [ + { + "key": "Authorization", + "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2ODY4MDk4MjEsImV4cCI6MTY4NjgxMzQyMX0.7FSgcgDmtOVIBJKJAW97Cu0sK10uIvYvTl-ZSiCU06M", + "type": "text" + } + ], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "bookName", + "value": "你", + "type": "text" + } + ] + }, + "url": { + "raw": "https://localhost:8443/book/findbook", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "8443", + "path": [ + "book", + "findbook" + ] + } + }, + "response": [] + }, + { + "name": "borrowBook", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2ODY4MTk0MzYsImV4cCI6MTY4NjgyMzAzNn0.SOR2I4IHdYLE79HCzcb1DYbFIe3rCnHGIM4IpWskvpU", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "bookId", + "value": "2", + "type": "text" + }, + { + "key": "userId", + "value": "10088", + "type": "text" + } + ] + }, + "url": { + "raw": "https://localhost:8443/borrow/borrowBook", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "8443", + "path": [ + "borrow", + "borrowBook" + ] + } + }, + "response": [] + }, + { + "name": "uploadImg", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2ODY5MjA5NDEsImV4cCI6MTY4NjkyNDU0MX0.XH5vdzNgleC44QsvYYfkASl1dV6Fl3kAnIDmbMJT0sc", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "formdata", + "formdata": [ + { + "key": "file", + "type": "file", + "src": "/D:/Word/Study/跨平台脚本/大作业/bigwork/asserts/img01.png" + } + ] + }, + "url": { + "raw": "https://localhost:8443/Img/upload", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "8443", + "path": [ + "Img", + "upload" + ] + } + }, + "response": [] + }, + { + "name": "downloadImg", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2ODY5MjgyMzUsImV4cCI6MTY4NjkzMTgzNX0.yd-5F_skT_YOoKkn6QSVeyDW6zYbK4UkPYX516C0G7o", + "type": "string" + } + ] + }, + "method": "GET", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "fileName", + "value": "0440d911e54fb.png", + "type": "text" + } + ] + }, + "url": { + "raw": "https://localhost:8443/Img/download", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "8443", + "path": [ + "Img", + "download" + ] + } + }, + "response": [] + }, + { + "name": "backBook", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "bookId", + "value": "2", + "type": "text" + }, + { + "key": "userId", + "value": "10088", + "type": "text" + } + ] + }, + "url": { + "raw": "https://localhost:8443/borrow/backBook", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "8443", + "path": [ + "borrow", + "backBook" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file -- Gitee