Reputation: 51
I'm new to Node and creating a simple form but I can't post using form in html but I can post in the rest client extension in vs code.
my index.js:
require('dotenv').config()
const express=require('express')
const app=express()
const mongoose=require('mongoose')
mongoose.connect(process.env.DATABASE_URL,{ useUnifiedTopology: true , useNewUrlParser: true } )
const db=mongoose.connection
db.on('error',()=>console.error(error))
db.once('open',()=>console.log('welcome to the database'))
app.use(express.json())
const authRoute=require('./routes/auth')
app.use('/auth',authRoute)
app.listen(3000,()=>console.log('welcome to the server'))
routes in auth
const express=require('express')
const app=express()
const router=express.Router()
const bcrypt=require('bcryptjs');
const Human=require('../model/human')
app.set('view-engine','ejs')
app.use(express.urlencoded({extended:false}))
//register
router.get('/register',(req,res)=>{
res.render('register.ejs')
})
router.post('/register',async(req,res)=>{
const hashedpass=await bcrypt.hash(req.body.password,10)
const newHuman=new Human({
name:req.body.name,
email:req.body.email,
password:hashedpass
})
try{
await newHuman.save()
res.redirect('/auth/login')
}catch(err){
res.send('errorr')
}
})
here is the register form
<h1>Register</h1>
<form method="POST" action="/auth/register">
<div>
<label for="name">name</label>
<input type="text" name="name" id="name" required></input>
</div>
<div>
<label for="email">email</label>
<input type="email" name="email" id="email" required></input>
</div>
<div>
<label for="password">password</label>
<input type="password" name="password" id="password" required></input>
</div>
<button type="submit">register</button>
</form>
<a href="/auth/login"></a>
Where did I go wrong? I think I lack a middleware in the form or something because as i said it works in the rest client extension
here is the error:(node:8680) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8680) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Upvotes: 0
Views: 186
Reputation: 51
I finally found the answer, it is because of the new update,install the new body parser(now seperate from express) and put the urlencodedparser to the post
const express=require('express')
const app=express()
const router=express.Router()
var bodyParser = require('body-parser')
const bcrypt=require('bcryptjs')
const Human=require('../model/human')
app.set('view-engine','ejs')
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(bodyParser.urlencoded({ extended: true }));
//register
router.get('/register',async(req,res)=>{
try{
res.render('register.ejs')
}catch(err){
res.json(err.message)
}
})
router.post('/register',urlencodedParser,async(req,res)=>{
const hashedpass=await bcrypt.hash(req.body.password,10)
try{
const newHuman=new Human({
name:req.body.name,
email:req.body.email,
password:hashedpass
})
const hum= await newHuman.save()
console.log(hum)
res.json(hum)
}catch(err){
res.json(err.message)
}
})
Upvotes: 1
Reputation: 177
Your form action points to a route called /register
, but your HTML form points to /auth/register
.
Upvotes: 0