如何在 HTML 中对 .ejs 文件进行 href

我正在尝试使用 Nodejs 和 Express 构建一个登录系统。我有一个登录页面,其中包含使用 ejs 的登录表单。

在我的index.html文件中,有一个按钮应该将用户引导至该登录页面:

<button id="loginBtn">

<a href="views/login.ejs"><LI> LOGIN </LI></a>

</button>

但它打印登录页面的 ejs 代码而不是显示实际页面。有人知道我在做什么吗?



蓝山帝景
浏览 72回答 2
2回答

温温酱

要渲染视图,您只需在expressjs中指定一个端点并仅调用该端点您不应该尝试调用.ejs文件位置示例:在这段代码中login.ejs会自动在后台渲染,服务器库会做这件事,res.render('login')会自动调用login.ejs&nbsp;router.get('/home', function(req, res, next) {&nbsp; &nbsp; res.render('login');&nbsp;});您的超链接应该单独调用路由器映射端点&nbsp;<a href="/home"><li> LOGIN </li></a>一旦你决定使用EJS,出于UI设计的目的,如果单独给你UI设计部分,那么你可以做一个虚拟代码来渲染一个带有固定数据的EJS,你不必用DB运行整个网站例如:假设有一个数据将渲染 EJS,要使用虚拟数据渲染,您可以这样做从正在尽自己职责的开发人员那里获取数据并执行此代码&nbsp;router.get('/home', function(req, res, next) {&nbsp; &nbsp; var data = {"name": "Emmanuel"}&nbsp; &nbsp; res.render('login', data);&nbsp;});正如你所看到的,数据是直接编码的temporary

叮当猫咪

这是一个相当老的问题,但我在为类似问题寻找自己的解决方案时发现了OPs帖子。&nbsp; // this link should take me to the 'new post' page which is rendered only from an ejs template (no HTML) and has a matching endpoint on my express router obj as 'new'&nbsp; <a href="new" class="new-post-link">new post</a>&nbsp; // clicking this link would move the user to the /new endpoint and would render the 'new post' page upon clicking the link&nbsp; <a href="new" class="new-post-link">new post</a>&nbsp; // for OP's specific problem:&nbsp; // original&nbsp; <button id="loginBtn">&nbsp; &nbsp; <a href="views/login.ejs"><LI> LOGIN </LI></a>&nbsp; </button>&nbsp;&nbsp;&nbsp; // solution&nbsp; <button id="loginBtn">&nbsp; &nbsp; <a href="login"><LI> LOGIN </LI></a>&nbsp; </button>我希望它能帮助某人。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5