如何在node / express js中访问hbs引擎视图目录中文件夹内的文件
Posted
技术标签:
【中文标题】如何在node / express js中访问hbs引擎视图目录中文件夹内的文件【英文标题】:how to access a file within a folder in view directory of hbs engine in node/express js 【发布时间】:2021-07-24 01:02:04 【问题描述】:希望你们都做得很好,我是 Expressjs 的新手并在其中做一个项目。 实际上,我在渲染视图目录中名为 index.hbs 的文件夹中的文件时遇到问题, 当我尝试访问时发现此错误: 错误:无法在视图目录“C:\Users\HBK1007\Desktop\ExpressjsFyPEPakTourisum\template\views”中查找视图“/blogger_dasboard”
我还附上了文件夹结构图像和我的代码
这里是代码enter image description here `
const express = require("express")
const app = express()
const path = require('path')
const port = 8000;
const hbs =require('hbs')
const bcrypt =require("bcryptjs")
// establishing db connection
require('./db/db_connec');
// Getting the collection of DB
const Registration=require('./models/registrationdb')
const NewsletterSubsciber=require('./models/newsletter')
const ContactUs_Client =require("./models/contactus")
// this is for DB data conersions
app.use(express.json());
app.use(express.urlencoded(extended:false))
//public static port
const static_path = path.join(__dirname, '../public')
const tempalte_path = path.join(__dirname, '../template/views')
const bloggerdashboard_path = path.join(__dirname, '../template/views/blogger_dashboard')
const partials_path =path.join(__dirname,'../template/particles')
app.set('view engine', 'hbs');
app.set('views', tempalte_path)
hbs.registerPartials(partials_path)
app.use(express.static(static_path))
// BloggerDashboard Routes
app.get('/blogger_dashboard', (req, res) =>
res.render('/blogger_dasboard')
)
app.listen(port, () =>
console.log("Listning to portss ")
)
【问题讨论】:
这些答案解决了你的问题吗? 【参考方案1】:我镜像了你的项目,你有几个错误:
-
错误的路径,太多了:
static_path
、tempalte_path
、bloggerdashboard_path
、partials_path
。
res.render("/blogger_dasboard")
像这样插入:res.render("blogger_dashboard")
;没有 slash,你错过了 h 字母。
下面没有static_path
bcrypt
和db
的工作示例(原因很明显)..
const express = require("express");
const app = express();
const path = require("path");
const port = 8000;
const hbs = require("hbs");
// const bcrypt = require("bcryptjs");
// establishing db connection
// require("./db/db_connec");
// Getting the collection of DB
// const Registration = require("./models/registrationdb");
// const NewsletterSubsciber = require("./models/newsletter");
// const ContactUs_Client = require("./models/contactus");
// this is for DB data conersions
// app.use(express.json());
app.use(express.urlencoded( extended: false ));
//public static port
// const static_path = path.join(__dirname, "./public");
const tempalte_path = path.join(__dirname, "./template/views");
const bloggerdashboard_path = path.join(
__dirname,
"./template/views/blogger_dashboard.hbs"
);
const partials_path = path.join(__dirname, "./template/particles");
app.set("view engine", "hbs");
app.set("views", tempalte_path);
hbs.registerPartials(partials_path);
// app.use(express.static(static_path));
// BloggerDashboard Routes
app.get("/blogger_dashboard", (req, res) =>
res.render("blogger_dashboard");
);
app.listen(port, () =>
console.log(`Server is listening at http://localhost:$port`);
);
我的项目文件夹和文件结构:
【讨论】:
以上是关于如何在node / express js中访问hbs引擎视图目录中文件夹内的文件的主要内容,如果未能解决你的问题,请参考以下文章
如何在node / express js中访问hbs引擎视图目录中文件夹内的文件
如何检查用户是不是在 Firebase 和 Express/Node.js 中经过身份验证?