×

Html & Html5

The World's best Software Development Study Portal




Node Js Path Connection:



Now create a view folder if you wants to use hbs
inside view create
index.hbs
register.hbs
npm i hbs
//install hbs package from terminal
Before moving ahead change the name of view to templates. And then create
a folder view and partials. And move your all files from templates to views.
Now make changes in app.js to connect index.hbs and register.hbs
const template_path=path.join(__dirname,"../templates/views")
app.set("view engine", "hbs");
app.set("views", template_path);
If You wants to include navbar in all page then create a folder
create a file navbar in partials folder then connect it to app.js
const hbs = require("hbs");
hbs.registerPartials(partials_path);


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

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 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(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.listen(port,()=>{
console.log(`Server is runung on port no ${port}`);
})