javascript-nodemon 应用程序崩溃 - 在启动之前等待文件更改...... MySQL,ReactJS
发布时间:2022-05-07 14:46:22 214
相关标签: # 前端
尝试使用 nodejs、express.js 和 mysql 登录 ReactJS 应用程序。此处注册正在工作,但在登录期间出现问题。这是我的登录代码
function Login(){
const [email, setEmail] = useState('');
const [pass, setPass] = useState('');
const navigate = useNavigate();
const login = () => {
Axios.post("http://localhost:3001/api/login", {
email: email,
pass: pass
}).then(()=>{
navigate('/info');
})
}
return(
LOGIN
)
}
export default Login;
服务器js
const express = require('express');
const app = express();
const mysql = require('mysql');
const cors = require('cors');
const { response } = require('express');
const session = require('express-session');
const path = require('path');
const db = mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'travel'
});
app.use(cors());
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(express.json());
app.use(express.urlencoded({extended: true}))
//Login
app.post("/api/login", (req,res)=> {
const email = req.body.email;
const pass = req.body.pass;
if(email && pass){
db.query('SELECT * FROM users WHERE email = ? AND pass = ?', [email, pass],(err, result) => {
if(err) throw err;
if(result.lenght > 0){
req.session.loggedin = true;
req.session.email = email;
response.redirect('/info');
}else{
response.send('Incorrect email and/or password!');
}
response.end();
});
}else{
response.send('Please enter email and password');
response.end();
}
});
app.listen(3001, ()=>{
console.log('running on port 3001');
});
我试着用其他方法来解决这个问题。但无法解决这个问题。它在注册时工作。但不适用于登录。
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报