feat(server.tests.test6): 新增 nodejs 实验 6

This commit is contained in:
2025-11-25 11:21:14 +08:00
parent 6a08063952
commit 65d3c97e60
4 changed files with 127 additions and 0 deletions

2
Server/tests/test6/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
node_modules
pnpm-lock.yaml

View File

@@ -0,0 +1,54 @@
const express = require("express");
const session = require("express-session");
const bodyParser = require("body-parser");
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(session({
secret: "hello kitty",
resave: false,
saveUninitialized: true,
cookie: { secret: false }
}))
const isAuthenticated = (req, res, next) => {
if (req.session.user)
next();
else
res.status(401).send("You are not authenticated!");
}
app.post("/login", (req, res) => {
const { username, password } = req.body;
if (username === "admin" && password === "password") {
req.session.user = { id: 1, username };
res.send("User logged in");
} else {
res.status(401).send("Invaild credentials");
}
})
app.post("/logout", (req, res) => {
req.session.destroy((error) => {
if (error)
return res.status(500).send("Could not log out.");
res.send("Logout successful")
})
})
app.get("/protected", isAuthenticated, (req, res) => {
res.send("This is a protected route");
})
app.get("/profile", isAuthenticated, (req, res) => {
res.send(`Welcome ${req.session.user.username}`);
})
app.listen(port, (error) => {
if (error)
console.log(error);
console.log(`server is running: http://localhost:${port}`);
})

View File

@@ -0,0 +1,52 @@
const express = require("express");
const bodyParser = require("body-parser");
const jwt = require("jsonwebtoken");
const app = express();
app.use(bodyParser.json());
const generateToken = (user) => {
return jwt.sign({ user }, "hello kitty", { expiresIn: "1h" })
}
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(" ")[1];
if (token == null)
return res.sendStatus(401);
jwt.verify(token, "hello kitty", (error, user) => {
if (error)
return res.sendStatus(401);
req.user = user;
next();
})
}
app.post('/jwt-login', (req, res) => {
const { username, password } = req.body;
if (username == "admin" && password == "password") {
const user = { id: 1, username };
const accessToken = generateToken(user);
res.json(accessToken);
} else {
res.status(401).send("Invaild credentials")
}
})
app.post("/jwt-logout", authenticateToken, (req, res) => {
res.send("Logged out successful");
})
app.get("/jwt-profile", authenticateToken, (req, res) => {
res.send(`Welcome ${req.user.user.username}`)
})
app.listen(3000, (error) => {
if (error)
console.log(error);
console.log("Server is running: http://localhost:3000");
})

View File

@@ -0,0 +1,19 @@
{
"name": "test6",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"packageManager": "pnpm@10.14.0",
"dependencies": {
"body-parser": "^2.2.1",
"express": "^5.1.0",
"express-session": "^1.18.2",
"jsonwebtoken": "^9.0.2"
}
}