简体   繁体   English

我的node.js用户注册不适用于“ / auth / registration”,但适用于“ / registration”

[英]my node.js user registration is not working with “/auth/registration” but works with “/registration”

I get error cannot POST /auth/auth/register with my code but when I change 我收到错误,无法通过我的代码POST /auth/auth/register ,但是当我更改时
app.get('/auth/register') to app.get('/register') the code works fine. app.get('/auth/register') app.get('/register')的代码可以正常工作。 Can anyone please tell me where the problem is and it would be a great help if anyone help me in solution . 任何人都可以告诉我问题出在哪里,如果有人帮助我解决问题,那将是一个很大的帮助。

I have tried changing app.get('/auth/register') to other routes like ('/create/registration'), app.get('/Registering/user') but it doesn't work .Should I should use only app.get('/userRegister') or app.get('createNewUser') or app.get('goodRegister') 我尝试将app.get('/auth/register')更改为其他路由,例如('/create/registration')、app.get('/Registering/user'),但是它不起作用。我应该使用仅app.get('/userRegister')app.get('createNewUser')app.get('goodRegister')

const express = require('express');
const expressEdge = require('express-edge');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');

const userController = require('./controllers/createUser');
const storeUserController = require('./controllers/storeUser');

const app = new express();

app.get('/auth/register', userController);
app.post('/users/register', storeUserController);

const User = require('../database/models/User')

} }

//my controller
module.exports = (req, res) => {
User.create(req.body, (error, user) => {

    res.redirect('/')

})

} }

//my database model
  const bcrypt = require('bcrypt');
const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({

username: {
    type: String,
    required: true
},

email: {
    type: String,
    required: true,
    unique: true

},
password: {
    type: String,
    required: true
}

})

UserSchema.pre('save', function(next) {
const user = this
bcrypt.hash(user.password, 10, function(error, encrypted) {
    user.password = encrypted
    next()
})
})

module.exports = mongoose.model('User', UserSchema);  



app.listen(4000, () => {
console.log("application listening on port 4000")

}) })

//my view
<form action="users/register" method="POST"  encType="multipart/form-data">

I have solved this problem myself. 我已经解决了这个问题。 thanks to everyone for taking out time to check through and give one explanation or another. 感谢大家抽出时间来检查并给出一个或另一个解释。 the problem with the code is the path to my controller. 代码的问题是我的控制器的路径。 the name of the file should not have been User but user so I changed const User = require('../database/models/User') to const User = require('../database/models/User') in other to match with the name of the file under the database/model directory 文件的名称不应该是User而是user所以我将const User = require('../database/models/User')更改为const User = require('../database/models/User')与数据库/模型目录下的文件名匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM