我创建了一个登录页面作为一个小例子。 该表单询问常见的信息,例如用户名、密码等,它还要求用户确认密码,如果长度不同,它应该抛出一个错误,警告用户有关差异的信息。
如果我输入正确的password
,它正在工作。和confirm password
长度,
如果 password
则无法工作不等于confirm password
错误如下所示:
Error: Failed to lookup view "/register" in views directory "/home/emanuele/Desktop/cashman-tracker-dashboard/views" at Function.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:580:17) at ServerResponse.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/response.js:1012:7) at ServerResponse.res.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express-ejs-layouts/lib/express-layouts.js:77:18) at /home/emanuele/Desktop/cashman-tracker-dashboard/routes/users.js:30:7 at Layer.handle [as handle_request] (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/layer.js:95:5) at next (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/layer.js:95:5) at /home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:335:12) at next (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:275:10) at Function.handle (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:174:3) at router (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:47:12) at Layer.handle [as handle_request] (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:317:13) at /home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:284:7
下面是相关检查的代码:
users.js
const express = require('express');
const router = express.Router();
// Login Page
router.get('/login', (req, res) => res.render('login'));
// Register Page
router.get('/register', (req, res) => res.render('register'));
// Register Handle
router.post('/register', (req, res) => {
const { name, email, password, password2 } = req.body;
let errors = [];
// Check required fields
if (!name || !email || !password || !password2) {
errors.push({ msg: 'Please fill in all fields' });
}
// Check password
if (password !== password2) {
errors.push({ msg: 'Passwords do not match' });
}
// Check password length
if (password.length < 6) {
errors.push({ msg: 'Password should be at least 6 characters' });
}
if (errors.length > 0) {
res.render('register', {
errors,
name,
email,
password,
password2
});
} else {
res.send('pass');
}
});
module.exports = router;
注册.ejs
<div class="row mt-5">
<div class="col-md-6 m-auto">
<div class="card card-body">
<h1 class="text-center mb-3"><i class="fas fa-user-plus"></i>Register</h1>
<% include ./partials/messages %> //<-- This seems to be the cause of error
<form action="/users/register" method="POST">
<div class="form-group">
<label for="name">Name</label>
<input
type="name"
id="name"
name="name"
class="form-control"
placeholder="Enter Name"
value="<%= typeof name != 'undefined' ? name : '' %>"
/>
</div>
<div class="form-group">
<label for="email">Email</label>
<input
type="email"
id="email"
name="email"
class="form-control"
placeholder="Enter Email"
value="<%= typeof email != 'undefined' ? email : '' %>"
/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input
type="password"
id="password"
name="password"
class="form-control"
placeholder="Create Pasword"
value="<%= typeof email != 'undefined' ? password : '' %>"
/>
</div>
<div class="form-group">
<label for="password2">Confirm Password</label>
<input
type="password"
id="password2"
name="password2"
class="form-control"
placeholder="Confirm Pasword"
value="<%= typeof email != 'undefined' ? password2 : '' %>"
/>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
<p class="lead mt-4">Have An Account? <a href="/users/login">Login</a></p>
</div>
</div>
</div>
messages.ejs
<% if(typeof errors != 'undefined'){ %>
<% errors.forEach(function(error) { %>
<%= error.msg %>
<% }); %>
<% } %>
如果有用的话,我还附上了我的项目树的打印屏幕:
问题似乎是register.ejs中的以下语句,但我不知道
<% include ./partials/messages %>
我不知道messages.ejs是否也与该错误有关
到目前为止我所做的事情:
我查看了不同的来源,并且 the official documentation是我用过的。我仔细检查了很多次,但错误仍然存在,一旦我输入长度为 X 的密码并确认长度为 X+1 的密码,我就会遇到那个奇怪的错误。
我不确定该错误是否与 messages.ejs 或语句 <% include ./partials/messages %>
有关。或者如果它可能是其他东西。
感谢您为我指明解决此问题的正确方向。
最佳答案
我已经尝试过你的代码,它工作正常,唯一不工作的一行是:
<% include ./partials/messages %>
您必须使用以下代码更改此行:
<%- include('partials/messages') %>
希望对你有帮助。
关于javascript - Node js - ejs : How to run password check on login/register form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60082729/