我正在尝试使用Express和Passport创建一个Node.js登录系统,但出现错误

时间:2019-11-08 14:08:43

标签: node.js express npm passport.js

我已经在package.json中更新了所有依赖项,并安装了所有需要的依赖项。 我已经从youtube尝试过,并编辑了我的app.js文件

C:\nodeproject\nodeauth\app.js:45
app.use(expressValidator({
        ^

TypeError: expressValidator is not a function
    at Object.<anonymous> (C:\nodeproject\nodeauth\app.js:45:9)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\nodeproject\nodeauth\bin\www:7:11)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodeauth@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nodeauth@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

但是当我尝试通过放置来运行文件时 npm启动命令

这些错误出现

{{1}}

我什至已经检查了Express Validator模块,但似乎没有任何解决办法。

1 个答案:

答案 0 :(得分:1)

See below example
// ...rest of the initial code omitted for simplicity.
const { check, validationResult } = require('express-validator');

app.post('/user', [
  // username must be an email
  check('username').isEmail(),
  // password must be at least 5 chars long
  check('password').isLength({ min: 5 })
], (req, res) => {
  // Finds the validation errors in this request and wraps them in an object with handy functions
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
  }

  User.create({
    username: req.body.username,
    password: req.body.password
  }).then(user => res.json(user));
});