update webs docs

This commit is contained in:
lzx
2025-04-21 11:41:18 +08:00
parent e4be66c067
commit 693f2ad1f5
7 changed files with 1678 additions and 0 deletions

2
nicetechcg/server/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
node_modules/*
package-lock.json

View File

@@ -0,0 +1,37 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const { expressjwt: jwtMiddleware } = require('express-jwt'); // 导入 express-jwt 中间件
const app = express();
const secret = 'nicetechsg';
// 中间件:保护所有 /api 路由
app.use(
'/api',
jwtMiddleware({ secret, algorithms: ['HS512'] })
);
app.get("/test", (req, res) =>{
res.json({message: "test"})
})
// 登录接口(生成 token
app.post('/login', (req, res) => {
const user = { password: 1, username: 'alice' };
const token = jwt.sign(user, secret, { expiresIn: '1h' });
res.json({ token });
});
// 受保护接口
app.get('/api/protected', (req, res) => {
res.json({ message: 'You have access!', user: req.auth });
});
// 错误处理
app.use((err, req, res, next) => {
if (err.name === 'UnauthorizedError') {
return res.status(401).json({ message: 'Invalid token' });
}
next(err);
});
app.listen(3000, () => console.log('Server started on port 3000'));

1539
nicetechcg/server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,17 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^5.1.0",
"express-jwt": "^8.5.1",
"jsonwebtoken": "^9.0.2"
}
}

View File

@@ -0,0 +1,38 @@
class Response {
constructor(data){
this.code = 200
this.msg = 'success'
// this.data = data
}
}
class SuccessResponse extends Response {
constructor(data){
super(data)
this.data = data
}
}
class ErrorResponse extends Response {
constructor(data){
super(data)
if(typeof data === 'string'){
this.code = 500
this.msg = data
}
if (typeof data === 'object'){
this.code = 500
this.msg = "error"
this.data = data
}
if (data === "" || data === null || data === undefined){
this.code = 500
this.msg = "error"
}
}
}
module.exports = {
SuccessResponse,
ErrorResponse
}