×

Html & Html5

The World's best Software Development Study Portal




Node Js Post Method:



//creater a Post method for Node Js in app.js File.

app.post("/register",async(req,res)=>{
try{
const registerEmp= new Register({
fullName:req.body.fullName,
username:req.body.username,
email:req.body.email,
password:req.body.password
})
const registered= await registerEmp.save();
console.log(registered)
res.status(201).render("index");
}catch(error){
res.status(400).send(error)
}
});
------------------------------------------------------------------------------

const express = require ("express")

const mongoose = require ("mongoose")

const dotenv = require('dotenv')

const app = express();

const path= require("path");

const hbs = require("hbs");

const port = process.env.PORT || 3000

const cors = require('cors')

const https = require('https');

const Register=require("./src/models/register");

dotenv.config()

dotenv.config({ path: "./env" })

//require("./src/db/con2");

require("./src/db/conn");

//mongoose.connect(process.env.DATABASE_ACCESS,()=>console.log("Database Connected"))

const static_path=path.join(__dirname,"../public")

const template_path=path.join(__dirname,"../df/templates/views")

const partials_path=path.join(__dirname,"../df/templates/partials")

app.use(cors());

app.use(express.json());``

app.use(express.urlencoded({

extended:false

}));

app.use(express.static(static_path))

app.set("view engine", "hbs");

app.set("views", template_path);

hbs.registerPartials(partials_path);

//console.log(path.join(__dirname,"../public"))

app.get("/",(req,res)=>{

res.render("index")

});

app.get("/register",(req,res)=>{

res.render("register")

});

app.post("/register",async(req,res)=>{

try{

const registerEmp= new Register({

fullName:req.body.fullName,

username:req.body.username,

email:req.body.email,

password:req.body.password

})

const registered= await registerEmp.save();

console.log(registered)

res.status(201).render("index");

}catch(error){

console.log(error)

res.status(400).send(error)

}

});

app.get('/list', (req, res) => {

Register.find((err, docs) => {

if (!err) {

{ sort: { _id: -1 } }

res.render("list", {

list: docs

});

}

else {

res.render("index")

}

}).sort({'_id':-1});

});

app.listen(port,()=>{

console.log(`Server is runung on port no ${port}`);

})



------------------------------------------------------------------------------

npm i [email protected]

-------------------------------------------------------------------------------





app.get('/list', async (req, res) => {

const docs = await Register.find();

if (!docs) {

res.render('index');

} else {

res.render('list', {

list: docs

});

}

});



List.hbs



< div class="container">
< div class= "row">
< div class ="col-sm-6">
< table class="table table-striped table-bordered">
< thead>
< tr>
< th>FullName
< th>Username
< th>Email
< th>Password
< /tr>
< /thead>
< tbody>
{{#each list}}
< tr>
< /td>
< /div>
< tr>
< td>{{this.fullName}}
< td>{{this.username}}
< td>{{this.email}}
< td>{{this.password}}
< /tr>
{{/each}}
< /tbody>
< /table>
< /div>
< /div>
< /div>
< /div>

< /div>
< /div>
< /div>
< /div>