html/index.js
2024-10-22 15:08:26 +08:00

41 lines
1.1 KiB
JavaScript

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}`);
});