- Router.post ('/register ',function (req,res,next) {
- var restresult = new Restresult ();
- var mobile = Req.body.mobile;
- if (!/1\d{10}/.test (mobile)) {//mobile number format check
- Restresult.errorcode = Restresult.illegal_argument_error_code;
- Restresult.errorreason = "Please fill in the true phone format";
- Res.send (Restresult);
- return;
- }
- var password = Req.body.password;
- if (!password | | Password.length < 6) {//password length check
- Restresult.errorcode = Restresult.illegal_argument_error_code;
- Restresult.errorreason = "Password length must not be less than 6 bits";
- Res.send (Restresult);
- return;
- }
- //findone method, the first parameter number condition, the second parameter is the field projection, the third parameter is the callback function
- Userentity.findone ({mobile:mobile},' _id ',function (err,user) {
- if (err) {//query exception
- Restresult.errorcode = Restresult.server_exception_error_code;
- Restresult.errorreason = "Server Exception";
- Res.send (Restresult);
- return;
- }
- if (user) {//Phone number registered
- Restresult.errorcode = Restresult.business_error_code;
- Restresult.errorreason = "Mobile phone number is registered";
- Res.send (Restresult);
- return;
- }
- var registeruser = New Userentity ({Mobile:mobile,password:password});
- //How to save an instance of a calling entity
- Registeruser.save (function (err,row) {
- if (err) {//server Save exception
- Restresult.errorcode = Restresult.server_exception_error_code;
- Restresult.errorreason = "Server Exception";
- Res.send (Restresult);
- return;
- }
- Res.send (Restresult); //Return successful results
- });
- });
- });
- Landing route
- Router.post ('/login ',function (req,res,next) {
- var restresult = new Restresult ();
- var mobile = Req.body.mobile;
- if (!/1\d{10}/.test (mobile)) {//mobile number format check
- Restresult.errorcode = Restresult.illegal_argument_error_code;
- Restresult.errorreason = "Please fill in the true phone format";
- Res.send (Restresult);
- return;
- }
- var password = Req.body.password;
- if (!password) {
- Restresult.errorcode = Restresult.illegal_argument_error_code;
- Restresult.errorreason = "Password cannot be empty";
- Res.send (Restresult);
- return;
- }
- Userentity.findone ({mobile:mobile,password:password},{password:0},function (err,user) {
- if (err) {
- Restresult.errorcode = Restresult.server_exception_error_code;
- Restresult.errorreason = "Server Exception";
- Res.send (Restresult);
- return;
- }
- if (!user) {
- Restresult.errorcode = Restresult.business_error_code;
- Restresult.errorreason = "User name or password error";
- Res.send (Restresult);
- return;
- }
- Restresult.returnvalue = user;
- Res.send (Restresult);
- //Update last Login time
- Userentity.update ({_id:user._id},{$set: {lastlogintime: new Date ()}}). exec ();
- });
- });
- Module.exports = router;
Restresult.js (Unified return data format) file contents are as follows:
[JavaScript]View PlainCopy
- var Restresult = function () {
- This.errorcode = Restresult.no_error;
- this.returnvalue = {};
- This.errorreason = "";
- };
- Restresult.no_error = 0; //No error
- Restresult.illegal_argument_error_code = 1; //Invalid parameter error
- Restresult.business_error_code = 2; //Business error
- Restresult.auth_error_code = 3; //Authentication error
- Restresult.server_exception_error_code = 5; //server Unknown error
- Restresult.target_not_exit_error_code = 6; //target does not exist error
- Module.exports = Restresult;
Mobile Phone number format check