mysql - react-express 应用程序中的 POST 请求的 URL 中显示了表单详细信息
问题描述
我正在制作用户身份验证服务器,但是在为新用户创建帐户时,表单中填写的详细信息在提交表单时在 URL 中可见,为什么?但是数据也被添加到数据库中。
我对该表单的反应前端是:
import {React,Component } from 'react';
import axios from 'axios'
import './form.css';
export default class signIn extends Component{
constructor(props){
super(props);
this.state={
Name:'',
Email:'',
Password:'',
ConfirmPassword:'',
Response:''
}
this.handleClick = this.handleClick.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleClick(e){
this.setState({
[e.target.name]:e.target.value
})
}
handleSubmit(e){
axios.post('http://localhost:5000/createUser',{
Name:this.state.Name,
Email:this.state.Email,
Password:this.state.Password,
ConfirmPassword:this.state.ConfirmPassword
}).then((res)=>{
this.setState({
Name:'',
Email:'',
Password:'',
ConfirmPassword:'',
Response:res.data
})
}).catch((err)=>{
console.log(err);
})
}
render(){
return (
<div>
<h5 className="message">{this.state.Response}</h5>
<div className="contact">
<section className="cntct">
<form >
<input type="text" name="Name" placeholder="Enter Your Full Name" onChange={this.handleClick} value={this.state.Name} required/>
<input type="email" name="Email" placeholder="Email Address" onChange={this.handleClick} value={this.state.Email} required/>
<input type="password" name="Password" placeholder="Password" onChange={this.handleClick} value={this.state.Password} required/>
<input type="password" name="ConfirmPassword" placeholder="Confirm Password" onChange={this.handleClick} value={this.state.ConfirmPassword} required/>
<button className="logIn" onClick={this.handleSubmit}>Create Account</button>
</form>
</section>
</div>
</div>
)
}
}
我的快递后端有以下代码:
const express = require('express')
const bodyParser = require('body-parser')
var sha1 = require('sha1');
var cors = require('cors')
const mySql = require('mysql');
const app = express();
const PORT = process.env.PORT||5000
app.use(cors())
app.use(bodyParser.json())
//Database
const pool = mySql.createPool({
connectionLimit:1000,
host:'localhost',
user:'root',
password:'',
database:'nodejsauthentication'
})
//Creating New Accounts
function createUser(req,res,next){
if(req.body.Name ==""||req.body.Email ==""||req.body.Password==""||req.body.ConfirmPassword==""){
res.send("All fields are necessary");
}
else if(req.body.Password ==""||req.body.ConfirmPassword==""){
res.send("Passwords donot match")
}
else{
pool.getConnection((err,connection)=>{
if(err){
throw err;
}
else{
connection.query(`SELECT * FROM USERS WHERE email='${req.body.Email}'`,(err,result)=>{
if(err){
throw err;
}
else if(result ==""){
connection.query(`INSERT INTO USERS (name,email,password) VALUES('${req.body.Name}','${req.body.Email}','${req.body.Password}')`,(err,result)=>{
connection.release()
if(err){
throw err
}
else{
res.send("Sucessfully Created a User")
}
})
}
else{
res.send("An user already exist with that email")
}
})
}
})
}
}
//MiddleWare for logging In.
function authenticate(req,res,next)
{
pool.getConnection((err,connection)=>{
if(err) throw err
connection.query(`SELECT * FROM USERS WHERE email='${req.body.Email}'`,(err,result)=>{
if(err) throw err
if(result =="")
res.send("No user is associated with that email")
else{
connection.query(`SELECT * FROM USERS WHERE email='${req.body.Email}' AND password='${req.body.Password}'`,(err,result)=>{
connection.release();
if(err) throw err
else{
if(result =="")
{
res.send("Email or password Didnot Match");
}
else{
res.send("Congratulations!!!")
}
}
})
}
})
})
}
//requests
app.get('/',(req,res)=>{
res.send("At '/'");
})
app.post('/home',authenticate,(req,res)=>{
console.log("Recieved a post request");
})
app.post('/createUser',createUser,(req,res)=>{
console.log("creating new Users");
})
//listen to a server
app.listen(PORT,()=>{
console.log(`Server running sucessfully at port ${PORT}`)
})
在提交表单时,输入被添加到后端,但是我的 URL 更改为:
http://localhost:3000/signIn?Name=harry&Email=gmail119%40gmail.com&Password=abcde12345&ConfirmPassword=abcde12345
解决方案
推荐阅读
- excel - 有没有一种更快的方法可以使用 Excel VBA 将日期添加到某个未来日期
- react-native - 只创建一个始终使用 react-i18next 翻译其内容的 Text 组件是不是一个坏主意?
- php - isset 功能不起作用。块内的代码执行
- php - 如何使用 php 将 Mailchimp API 3.0 的“购物车”和“购物车行”添加到自定义商店?
- django - 启动 celery .delay 后 Django 冻结。怎么修?
- tinymce - TinyMCE 5:如何从菜单中删除字体名称和字体大小?
- android - 在安卓。检查移动数据是否打开或关闭
- python - 将“亚洲/加尔各答”时区日期时间转换为自纪元以来的毫秒数时出错
- python - scrapy:添加一些数学条件
- json - 在 postgres sql 中拆分 JSON 列