使用express-validator报这个错

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

慕仰4366054

2020-05-12

[root@fh comm-node]# node app.js
/root/comm-node/node_modules/express-validator/src/middlewares/one-of.js:7
return async (req, _res, next) => {
^
SyntaxError: Unexpected token (
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions…js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/root/comm-node/node_modules/express-validator/src/index.js:6:10)
at Module._compile (module.js:570:32)

我的router/user.js代码如下

const express = require('express')
const Result = require('../models/Result')
const { login } = require('../services/user')
const { md5 } = require('../utils')
const { PWD_SALT } = require('../utils/constant')
const { body, validationResult } = require('express-validator')
const boom = require('boom')


const router = express.Router()

// 嵌套路由
router.post(
    '/login',
    [
        body('username').isEmail().withMessage('username类型不正确'),
        body('password').isLength({ min: 6 }).withMessage('password类型不正确')
    ],
    (req, res, next) => {
        const err = validationResult(req)
        if (!err.isEmpty()) {
            const [{ msg }] = err.errors
            next(boom.badRequest(msg))
        } else {
            const username = req.body.username
            const password = md5(`${req.body.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
写回答

1回答

扬_灵

2020-05-13

同学你好,这个是插件报错,你可以试着删除node_modules文件夹后重新安装,或者可以将代码提交一下我在本地帮你看一下

0
4
扬_灵
回复
慕仰4366054
同学你好,这个是在项目启动的时候报错的吗?我在本地将你的代码注释去掉之后在安装依赖之后直接就启动了,没有你说的这个问题,你试试npm->cnpm-yarn这种覆盖安装的形式
2020-05-13
共4条回复

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

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

2829 学习 · 1714 问题

查看课程