How do i send a response to the front end whether or not the if statement is true
i tried to use res.status() but when i look at the response it has not changed
im recieving this response from the front end
Request URL: http://localhost:3000/register?
Request Method: GET
Status Code: 304 OK
even though im trying to get a response from the backend with either res.status(200)
or res.status(400)
const UserModel = require("./Server/models/User")
const Post = require("./Server/models/Post")
const mongoose = require("mongoose")
const bodyParser = require("body-parser")
const express = require("express")
const app = express()
mongoose.connect("mongodb://localhost:27017/BlogSite")
app.use(express.json())
app.get("/",(req,res)=> {
res.send("Home")
})
app.post("/api/register",(req,res)=> {
console.log(req.body)
const username =(req.body.username)
const password = (req.body.password)
UserModel.create({username:username,password:password},(err) => {
if (err) {
console.log(err)
}
else{
console.log("User Created")
res.status(200).send("Home");
}
})
});
app.post("/api/login",(req,res)=> {
UserModel.find({username: req.body.username,password :req.body.password},(err,data) =>{
if (data.length == 1){
console.log("Logged in")
app.post("/api/restatus",(req,res)=>{
console.log("sent")
})
}
else{
console.log("Wrong Email or password")
res.status(401)
}
})
})
app.listen(3001,()=> {
console.log("listening on port 3001")
})
Frontend
import React, { useState } from 'react';
import './App.css';
function Register() {
const [username,setUsername] = useState("")
const [password,setPassword] = useState("")
function handleSubmit(event) {
event.preventDefault
fetch("http://localhost:3001/api/register",{
method:"post",
headers:{
"Content-Type" : "application/json"
},
body:JSON.stringify({username:username,password:password})
})
.then(function(res){ console.log(res) })
.catch(function(res){ console.log(res) })
}
return (
<div className="App">
<form onSubmit={handleSubmit}>
<input placeholder="username" value={username} onChange={ e => setUsername(e.target.value)} />
<input placeholder="password " value={password} onChange={ e => setPassword(e.target.value)}/>
<button type='submit' >Submit</button>
</form>
</div>
);
}
export default Register;