安装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中的?去掉试一下。
012024-01-08
相似问题
回答 2
回答 2