Express JS验证程序功能错误器

时间:2018-06-20 10:01:02

标签: node.js express

我正在学习如何使用Express JS制作登录系统,并在Validator函数中遇到该错误,我不知道它有什么问题,语法没有错,并且在我正在观看的教程中也能正常工作在Github和其他网站上进行搜索时,我什么也没找到,当我问一个人时,他告诉我有关NodeJs版本的信息,但是我不确定,我不知道如何解决该问题。

这是我的错!

//the erorr msg

 erorrFormatter: fuction(param, msg, value){
                                              ^

SyntaxError: Unexpected token {
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)

这是我的代码:

//my server code

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var expressValidator = require('express-validator');
var LocalStrategu = require('passport-local').Strategy;
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 indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');


app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//Handel sessions
app.use(session({
        secret: 'secret',
        saveUninitialized: true,
        resave: true
        }));

//Passport
app.use(passport.initialize());
app.use(passport.session());

//Validator
app.use(expressValidator({
    erorrFormatter: fuction(param, msg, value){
        var namespace = param.split('.');
        var root = namespace.shift();
        var formOaram = root;
    while(namespace.length){
        formParam += '[' + namespace.shift() + ']';
}
    return{
        param : formParam,
        msg   : msg,
        value : value
     };
    }

}));

app.use(require('connect-flash')());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});



app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// 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;

1 个答案:

答案 0 :(得分:0)

对,在您的代码中,您具有以下内容;

erorrFormatter: fuction(param, msg, value){

其中缺少n,请替换为;

erorrFormatter: function(param, msg, value){

在这种情况下,简单的印刷错误,但是添加n应该可以很快解决