我遇到了这个无法解决的错误,并且对此感到困惑。
expressValidator({^ press \ lib \ application.js:TypeError: expressValidator不是函数
这是完整的错误报告:
C:\ Users \ Desktop \ src \ app.js:50 expressValidator({^ press \ lib \ application.js:TypeError:expressValidator不是 功能 在对象。 (C:\ Users \ Desktop \ src \ app.js:50:5):3) 在Module._compile(内部/模块/cjs/loader.js:776:30) 在Object.Module._extensions..js(内部/模块/cjs/loader.js:787:10) 在Module.load(internal / modules / cjs / loader.js:653:32) 在tryModuleLoad(内部/模块/cjs/loader.js:593:12) 在Function.Module._load(内部/模块/cjs/loader.js:585:3) 在Function.Module.runMain(内部/模块/cjs/loader.js:829:12) 在启动时(internal / bootstrap / node.js:283:19) 在bootstrapNodeJSCore(internal / bootstrap / node.js:622:3)
我做了一些与该错误相关的线程上建议的操作,但它们不能解决我的问题。
有人可以帮忙吗?
这是我的app.js代码
var express = require("express");
var path = require("path");
var favicon = require("serve-favicon");
var logger = require("morgan");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var session = require("express-session");
var passport = require("passport");
var LocalStrategy = require("passport-local").Strategy;
// var expressValidator = require("express-validator");
const { check, expressValidator } = require('express-validator');
var multer = require("multer");
var upload = multer({ dest: "./uploads" });
var flash = require("connect-flash");
var bcrypt = require("bcryptjs");
var mongo = require("mongodb");
var mongoose = require("mongoose");
var db = mongoose.connection;
var routes = 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");
// 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 }));
// Handle Sessions
app.use(
session({
secret: "secret",
saveUninitialized: true,
resave: true
})
);
// Passport
app.use(passport.initialize());
app.use(passport.session());
// Validator
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(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.get("*", function(req, res, next) {
res.locals.user = req.user || null;
next();
});
app.use("/", routes);
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 handlers
// development error handler
// will print stacktrace
if (app.get("env") === "development") {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: {}
});
});
module.exports = app;