安装npm i -S express-validator后,引入,启动后端项目报错

来源:10-7 express-validator 表单验证

慕姐9832373

2023-12-26

控制台使用npm i -S express-validator来安装
C:\Users\pp\IdeaProjects\admin-imooc-node>npm i -S express-validator

  • express-validator@7.0.1
    updated 1 package and audited 84 packages in 3.246s
    found 0 vulnerabilities

user.js代码,添加这一条:const { body } = require(‘express-validator’)
const express = require(‘express’)
const Result = require(’…/models/Result’)
const { login } = require(’…/service/user’)
const { md5 } = require(’…/utils/index’)
const { PWD_SALT } = require(’…/utils/constant’)
const { body } = require(‘express-validator’)

const router = express.Router()

router.post(’/login’, function(req, res, next) {
let { username, password } = req.body

password =md5(`${password}${PWD_SALT}`)
login(username, password).then(user => {
    if (!user || user.length === 0) {
        new Result('登录失败').fail(res)
    } else {
        new Result('登录成功').success(res)
    }
})

})

router.get(’/info’, function(req, res, next) {
res.json(‘user info…’)
})

module.exports = router

启动项目报错
C:\Users\pp\IdeaProjects\admin-imooc-node>node app.js
C:\Users\pp\IdeaProjects\admin-imooc-node\node_modules\express-validator\src\middlewares\exact.js:28
const locations = opts?.locations || [‘body’, ‘params’, ‘query’];
^

SyntaxError: Unexpected token '.'
at Module._compile (internal/modules/cjs/loader.js:881:18)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:962:10)
at Module.load (internal/modules/cjs/loader.js:798:32)
at Function.Module._load (internal/modules/cjs/loader.js:711:12)
at Module.require (internal/modules/cjs/loader.js:838:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (C:\Users\pp\IdeaProjects\admin-imooc-node\node_modules\express-validator\src\index.js:14:14)
at Module._compile (internal/modules/cjs/loader.js:945:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:962:10)
at Module.load (internal/modules/cjs/loader.js:798:32)

写回答

1回答

扬_灵

2023-12-27

同学你好,把opts?.locatons中的?去掉试一下。

0
1
慕姐9832373
没有解决,如果不引入不报错,一引入就报错启动不了,就跟这一行代码有关
2024-01-08
共1条回复

Vue Element+Node.js开发企业通用管理后台系统

基于Element的中后台课程,一套中小型企业通用的后台管理系统

2829 学习 · 1714 问题

查看课程