我正在尝试在包含所有空字段的 node.js
应用程序中提交注册表单。验证应重定向回错误的同一页面。但是找不到404
未找到消息的错误页面。该网页的网址也具有误导性。
http://localhost:3000/users/users/register。我不知道为什么会出现两次用户。get
请求正常。唯一问题是post
。
这是我的app.js代码:
var express = require('express');
var expressValidator = require('express-Validator');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer({ dest: './uploads' });
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
//app.use(multer({dest:'./uploads'}));
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
secret:'secret',
saveUnitialized:true,
resave:true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
这是我的邮政编码:
var express = require('express');
var router = express.Router();
router.post('/register', function(req, res, next) {
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password1 = req.body.password;
var password = req.body.password2;
req.checkBody('name','Name Field is required').notEmpty();
req.checkBody('email','Email Field is required').notEmpty();
req.checkBody('email','Email is incorrect format').isEmail();
req.checkBody('username','UserName is required').notEmpty();
req.checkBody('password','Password is required').notEmpty();
req.checkBody('password2','Password do not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
res.render('register',{
errors:errors,
name:name,
email:email,
username:username,
password:password,
password2:password2,
});
}
});
这是我的register.jade代码:
extends layout
block content
h1 Register
p Please register using the form below
ul.errors
if errors
each errors, i in errors
li.alert.alert-danger #{error.msg}
form(method='post',action='users/register')
.form-group
label Name
input.form-control(name='name',type='text',placeholder='Enter Name')
.form-group
label Email
input.form-control(name='email',type='email',placeholder='Enter Email')
.form-group
label UserName
input.form-control(name='username',type='text',placeholder='Enter UserName')
.form-group
label Password
input.form-control(name='password',type='password',placeholder='Enter Password')
.form-group
label Confirm Password
input.form-control(name='password2',type='password',placeholder='Confirm Password')
input.btn.btn-default(name='submit',type='submit',value='Register')
答案 0 :(得分:0)
请使用以下链接访问您要查找的页面
http://localhost:3000/users/register
在layout.jade上,确保您的导航链接如下
ul.navbar-nav.navbar-right
li.nav-item(class=(title == 'Members' ? 'active' : ''))
a.nav-link(href='/') Members
li.nav-item(class=(title == 'Register' ? 'active' : ''))
a.nav-link(href='/users/register') Register
li.nav-item(class=(title == 'Login' ? 'active' : ''))
a.nav-link(href='/users/login') Login
ul.nav.navbar-nav.navbar-right
li.nav-item
a.nav-link(href='/users/logout') Logout