Node.js 如何将用户重定向回注册页面,但必填字段旁边有红色星号?

Node.js 如何将用户重定向回注册页面,但必填字段旁边有红色星号?,node.js,express,Node.js,Express,我的目标很简单。如果用户试图注册并在所有必填字段都已满之前单击“提交”,我想将其重定向回注册页面,但稍微更改一下,以便在每个必填字段旁边显示一个红色星号(我知道,这是一个任意且丑陋的选择)。我想我不必为了这个目的而有一个单独的观点。是否可以以这种方式更改注册视图 在routes.js中 // process the signup form app.post('/signup', passport.authenticate('local-signup', { success

我的目标很简单。如果用户试图注册并在所有必填字段都已满之前单击“提交”,我想将其重定向回注册页面,但稍微更改一下,以便在每个必填字段旁边显示一个红色星号(我知道,这是一个任意且丑陋的选择)。我想我不必为了这个目的而有一个单独的观点。是否可以以这种方式更改注册视图

在routes.js中

// process the signup form
    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup?missingFields=true', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));
在signup.ejs中

% if (req.params.missingFields) {%>
        BLAH
    <% } %>
%if(请求参数缺失字段){%>
废话
我在signup.ejs中发现一个错误“在eval时未定义req”以下是完整的错误文本:

引用错误:/home/adam/Documents/Node/Node authentication/views/signup.ejs:26 24 |-->25 |>>26 | 27 | BLAH 28 | 29 | req未在eval处定义(eval at(/home/adam/Documents/Node/Node authentication/Node|modules/ejs/ejs/lib/ejs:237:14),:31:825)(/home/adam/Documents/Node/Node authentication/Node_modules/ejs/lib/ejs:237:14),:31:2194)在/home/adam/Documents/Node/Node authentication/Node_modules/ejs/ejs/lib/ejs:250:15 at Object.exports.render(/home/adam/Documents/Node/Node/Node authentication/Node/Node-modules/ejs/ejs:288:13)在View.exports.renderFile[作为引擎](/home/adam/Documents/Node/Node authentication/Node_modules/ejs/lib/ejs:318:20)在Function.app.render(/home/adam/Documents/Node authentication/Node/Node/Node authentication/Node_modules/express/lib/application.js:502:10)在ServerResponse.res.render的View(/home/adam/Documents/Node/Node/Node/Node/Node/Node/authentication/Node/在下一层的Object.handle(/home/adam/Documents/Node/Node authentication/Node_modules/express/lib/response.js:777:7)处(/home/adam/Documents/Node/authentication/Node_modules/express/lib/router/route.js:35:7)处


首先,您应该在发布或将用户发送到其他地方之前检查数据。一旦您认为数据正确并且通过了所有验证检查,您可以发布它

您可以使用简单的ajax检查字段,并将星号放在字段旁边

但是,如果由于某种原因无法执行此操作,则可以使用

res.redirect('/path')

并使用星号

res.redirect('/path?errorInField1=true')


您可以使用
req.params.errorInField1获取该数据,并在客户端执行相应的验证


在我看来,通过ajax提交请求会更好,如果您提交表单,当出现错误时,您必须重定向回去。

为页面的get方法创建一个post方法。首先安装body parser以获取post值

$ npm install --save body-parser
然后初始化它

var bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

app.use(express.json());
app.use(express.urlencoded());
现在验证用户输入

router.post('/register' function(req, res){
    var email = req.body.email;
    var name = req.body.name;
    ..... // Other required fields.
    if(email || name ...//Other variables to validate for null){
        req.flash('error', 'All the fields are required completing');
        req.redirect()
    }
});

我知道该怎么做,我在问如何将它们重定向回注册页面,但改变它的外观。我现在意识到我应该在客户端JS中这样做。在答案中添加了这一部分:)那么我需要使用decodeURIComponent来检查浏览器中的GET变量吗?不,不,使用
req.params.theirl,你想要的值
我还是不太明白。我如何将req发送回浏览器?在/path中运行的任何JS文件是否都可以访问req对象?为什么
mongodb
passport
包括标签?我正在使用这些标签,我想可能有一些我不知道的软件包可以帮助我。