feat: 添加 nodejs 实验 7 的第二部分以及课堂练习 3
This commit is contained in:
2
Server/3/.gitignore
vendored
Normal file
2
Server/3/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
node_modules
|
||||||
|
pnpm-lock.yaml
|
||||||
94
Server/3/app.js
Normal file
94
Server/3/app.js
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const session = require("express-session");
|
||||||
|
const bodyParser = require("body-parser");
|
||||||
|
const mysql = require("mysql2");
|
||||||
|
const cors = require("cors")
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
|
const db = mysql.createConnection({
|
||||||
|
host: "127.0.0.1",
|
||||||
|
port: 3306,
|
||||||
|
user: "root",
|
||||||
|
password: "123456",
|
||||||
|
database: "express_db"
|
||||||
|
})
|
||||||
|
|
||||||
|
db.connect((err) => {
|
||||||
|
if (err) {
|
||||||
|
console.log("Error connect to MySQL database:", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log("Connected to MySQL database");
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
app.use(cors({
|
||||||
|
origin: "http://127.0.0.1:3000",
|
||||||
|
method: ["GET", "POST"],
|
||||||
|
allowedHeaders: ['Content-Type', "Authorization"]
|
||||||
|
}))
|
||||||
|
app.use(session({
|
||||||
|
secret: "secret-key",
|
||||||
|
resave: false,
|
||||||
|
saveUninitialized: true,
|
||||||
|
cookie: { secret: false }
|
||||||
|
}));
|
||||||
|
|
||||||
|
const isAuthorization = (req, res, next) => {
|
||||||
|
if (req.session.user)
|
||||||
|
next();
|
||||||
|
else
|
||||||
|
res.status(401).send("You are not authorization")
|
||||||
|
};
|
||||||
|
|
||||||
|
app.post("/login", (req, res) => {
|
||||||
|
const { username, password } = req.body;
|
||||||
|
const sql = "SELECT username, password FROM users WHERE username = ? AND password = ?"
|
||||||
|
db.query(sql, [username, password], (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
res.status(500).send("query database failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.length > 0) {
|
||||||
|
req.session.user = result[0];
|
||||||
|
res.send("Login successful!");
|
||||||
|
} else {
|
||||||
|
res.send("Login failed.");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/register", (req, res) => {
|
||||||
|
const { username, password } = req.body;
|
||||||
|
const sql = "INSERT INTO users (username, password) VALUES (?, ?)"
|
||||||
|
db.query(sql, [username, password], (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
|
||||||
|
res.status(500).send("query database failed");
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send("Register successful!");
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/ucenter", isAuthorization, (req, res) => {
|
||||||
|
const user = req.session.user;
|
||||||
|
res.send(`hello ${user.username}, welcome to ucenter`);
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/orders", isAuthorization, (req, res) => {
|
||||||
|
const user = req.session.user;
|
||||||
|
res.send(`hello ${user.username}, welcome to orders`);
|
||||||
|
})
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server is running on http://localhost:${port}`);
|
||||||
|
|
||||||
|
})
|
||||||
20
Server/3/package.json
Normal file
20
Server/3/package.json
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "3",
|
||||||
|
"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",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"express": "^5.2.1",
|
||||||
|
"express-session": "^1.18.2",
|
||||||
|
"mysql2": "^3.16.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
.
|
.
|
||||||
├── 1 # 课堂练习1
|
├── 1 # 课堂练习1
|
||||||
├── 2 # 课堂练习2
|
├── 2 # 课堂练习2
|
||||||
|
├── 3 # 课堂练习3
|
||||||
└── tests # 实验文件夹
|
└── tests # 实验文件夹
|
||||||
├── test1 # 实验1
|
├── test1 # 实验1
|
||||||
├── test2 # 实验2
|
├── test2 # 实验2
|
||||||
@@ -21,5 +22,7 @@
|
|||||||
├── test5 # 实验5
|
├── test5 # 实验5
|
||||||
├── test6 # 实验6
|
├── test6 # 实验6
|
||||||
├── test7 # 实验7
|
├── test7 # 实验7
|
||||||
|
│ ├── 7.1 # 实验7.1
|
||||||
|
│ └── 7.2 # 实验7.2
|
||||||
└── test8 # 实验8
|
└── test8 # 实验8
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user