This commit is contained in:
ZZY 2024-10-22 15:08:26 +08:00
commit 5146808c2f
5 changed files with 129 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.*
!.gitignore
node_modules/
package-lock.json

41
index.js Normal file
View File

@ -0,0 +1,41 @@
const express = require('express');
const app = express();
const port = 8081;
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static('static'));
app.post('/login', (req, res) => {
const { username, password } = req.body;
// 模拟登录验证
if (username === '123' && password === '123') {
// res.redirect(302, '/success.html');
res.json({ success: true, redirectUrl: '/success.html' });
} else {
res.status(401).json({ success: false, message: 'Invalid credentials' });
}
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status(404);
res.end('<h1>404 Not Found</h1>');
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
console.log(err);
// render the error page
res.status(err.status || 500);
res.end('<h1>error</h1>');
});
app.listen(port, () => {
console.log(`Server running at http://127.0.0.1:${port}`);
});

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "html",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.21.1",
"nodemon": "^3.1.7"
}
}

57
static/index.html Normal file
View File

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
</head>
<body>
<form id="loginForm">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit" value="Submit">
</form>
<script>
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表单默认提交行为
const username = document.querySelector('input[name="username"]').value;
const password = document.querySelector('input[name="password"]').value;
const bodyData = new URLSearchParams({
username: username,
password: password
});
console.log(bodyData.toString());
fetch('/login', {
method: 'POST',
body: bodyData.toString(), // 发送表单数据
headers: {
'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
}
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Login failed');
}
})
.then(data => {
if (data.success) {
window.location.href = data.redirectUrl; // 跳转到指定URL
} else {
alert('Login failed');
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred during login');
});
});
</script>
</body>
</html>

11
static/success.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Successful</h1>
</body>
</html>