I have been working on a crud api using node and express I am trying to call a post request from an ejs file but it does not work.
This is the ejs file
<h1>Register</h1>
<form action="/register" method="POST">
<div >
<label for="name">Name</label>
<input type="text" name="name" id="name" required>
</div>
<div >
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
</div>
<div >
<label for="email">Email</label>
<input type="email" name="email" id="email" required>
</div>
<button type="submit">Register</button>
</form>
<a href="/login">Login</a>
this is the post request
router.post("/register", async (req,res) =>{
const hashedPassword= await bcrypt.hash(req.body.password,10)
const user = new User({
name: req.body.name,
password: hashedPassword,
email: req.body.email
})
try{
const newUser= await user.save()
res.status(201).json(newUser)
res.redirect("/login")
}catch (err){
res.status(400).json({message: err.message})
res.redirect("/register")
}
console.log(`User with the name of ${user.name} has been added to the database`)
})
This post request perfectly in postman and the info goes inside the database but when I try to do it through the browser it does not work.
I get no errors
Via Active questions tagged javascript - Stack Overflow https://ift.tt/9fnX31D
Comments
Post a Comment