У меня есть AuthenticationController
, который имеет метод signupWithCredentials
.У меня есть другой контроллер под названием TestController
, который работает правильно, хотя.
При создании route
я получаю эту ошибку:
router.post('/user/signup',
AuthenticationController.signupWithCredentials)
Это мои маршруты:
'use strict'
const { Router } = require('express')
const { TestController } = require('./../controllers/TestController')
const { AuthenticationController } = require('./../controllers/AuthenticationController')
const { AuthMiddleware } = require('./../middleware/AuthMiddleware')
const router = new Router()
router.get('/test', TestController.test)
router.post('/user/signup', AuthenticationController.signupWithCredentials)
module.exports = router
Это мой класс контроллера
'use strict'
const uuid = require('uuid/v4')
const bcrypt = require('bcrypt')
const config = require('./../config')
const { promiseEjs } = require('./../utils/promiseEjs')
const { mailSender } = require('./../utils/mailSender')
const { errorHandler } = require('./../utils/errorHandler')
const { User } = require('./../schema/user')
const { authMiddleWare } = require('./../middleware/AuthMiddleware')
class AuthenticationController {
/**
* API 1.0 | POST
* @example {
* firstName: String,
* lastName: String,
* email: String,
* password: String
* }
* @param {*} req
* @param {*} res
*/
static async signupWithCredentials (req, res) {
try {
let email = req.body.email
let user = await User.findOne({ email: email })
if (user) {
throw {
code: 400, message: 'User already exist'
}
}
else {
let emailVerificationToken = uuid()
user = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 8),
emailVerificationToken: emailVerificationToken
})
await user.save()
try {
let emailVerificationLink = `${config.SERVER_DEV_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}`
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') { emailVerificationLink = `${config.SERVER_PRODUCTION_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}` }
let html = await promiseEjs.renderFile('./emails/signup.ejs', { user: user, emailVerificationLink: emailVerificationLink })
await mailSender.sendMail(user.email, 'Welcome to HandyGems', html)
}
catch (error) {
console.log(error)
}
let authToken = authMiddleWare.createJWT(user)
await User.findOneAndUpdate({ _id: user._id }, { $set: { authToken: authToken } })
res.send({ authToken: authToken })
}
}
catch (error) {
errorHandler.sendError(res, error)
}
}
}
const authenticationController = new AuthenticationController()
module.exports = { authenticationController }