Node.js发布请求不断加载

时间:2018-09-05 18:25:00

标签: node.js express

当我尝试使用适当的JSON邮递员发布请求时,它会不断加载... 在Chrome控制台中,它显示404错误

这是运行服务器localhost:3000 / users的脚本 app.js

//Define express instance
const app = express();

//Define port number 
const port = 3000 ;

//Static folder for downloaded files
app.use(express.static(path.join(__dirname + "/public")));

//Route users Directory Setup
app.use("/users",users);


//Start server
app.listen(port , () => {
    console.log("Just started your server on port : " + port +" (localhost:3000)");
})

users.js 注册帖子,问题可能出在这里,想不通在StackOverflow上搜索了每个问题

//Register
router.post('/register', (req, res, next) => {

  let newUser = new User ({
    name : req.body.name,
    email : req.body.email,
    username : req.body.username,
    password : req.body.password
  });

  User.addUser(newUser, (err, user) => {
    if(err) {
      res.json({success: false, msg: 'Failed to register user'});
    } else {
      res.json({success: true, msg: 'User registered'});
    }
  });

});

user.js

// User Schema
const UserSchema = mongoose.Schema ({
  name: {
    type: String
  },
  email: {
    type: String,
    required: true
  },
  username: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  }
});

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

module.exports.addUser = function(newUser, callback) {
  bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(newUser.password, salt, (err, hash) => {
      if(err) throw err;
      newUser.password = hash;
      newUser.save(callback);
    });
  });
}

}

请求

发布localhost:3000 / users / register

{
    "name" : "houssam",
    "username" : "houssam11",
    "email" : "houssam.bb0101@gmail.com",
    "password" : "ssssssss1111"
}

1 个答案:

答案 0 :(得分:0)

如果您查看文档,它将显示从用户模型构造和保存文档的正确方法。 https://mongoosejs.com/docs/models.html。不要忘记在res语句前添加return

//Register
router.post('/register', (req, res, next) => {

  let newUser = new User ({
    name : req.body.name,
    email : req.body.email,
    username : req.body.username,
    password : req.body.password
  });

  newUser.save((err, user) => {
    if(err) {
      return res.json({success: false, msg: 'Failed to register user'});
    } 

    return res.json({success: true, msg: 'User registered'});

  });

});

现在,如果要在保存用户之前对密码进行哈希处理,则可以使用预保存的钩子https://mongoosejs.com/docs/middleware.html

// User Schema
const UserSchema = mongoose.Schema ({
  name: {
    type: String
  },
  email: {
    type: String,
    required: true
  },
  username: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  }
});

UserSchema.pre('save', function(next){
    var user = this;
    if (!user.isModified('password')) return next();

    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt){
        if(err) return next(err);

        bcrypt.hash(user.password, salt, function(err, hash){
            if(err) return next(err);

            user.password = hash;
            next();
        });
    });
});

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

}