Strategy
1.0.0
이 저장소는 Nodejs, Express, MongoDB 및 Ajax가 포함된 애플리케이션 소프트웨어이며, 이 애플리케이션에는 Express로 생성된 API와 Passport를 사용한 인증이 포함되어 있습니다.
Nodejs v10.3, Express, Mongoose, Passport, Bcrypt, Express-Validator, Connect-Flash, Express-Handlebars 등을 사용하는 것이 좋습니다.
MongoDB를 사용하는 것이 바람직합니다.
Postman 또는 RestEasy를 사용하여 API를 공급합니다.
$ git clone https://github.com/DanielArturoAlejoAlvarez/Strategy.git [NAME APP]
$ npm install
$ npm start다음 단계를 따르면 됩니다. 중요한:
...
router . post ( '/register' , ( req , res ) => {
let { name , email , username , password , password2 , avatar , role , age , state } = req . body
//Validations
req . checkBody ( 'name' , 'Name is required' ) . notEmpty ( )
req . checkBody ( 'email' , 'Email is not valid' ) . isEmail ( )
req . checkBody ( 'username' , 'Username is required' ) . notEmpty ( )
req . checkBody ( 'password' , 'Password is required' ) . notEmpty ( )
req . checkBody ( 'password2' , 'Password do not match' ) . equals ( req . body . password )
req . checkBody ( 'avatar' , 'Avatar is required' ) . notEmpty ( )
req . checkBody ( 'role' , 'Role is required' ) . isNumeric ( )
req . checkBody ( 'age' , 'Age is required' ) . isNumeric ( )
req . checkBody ( 'state' , 'State is required' ) . isBoolean ( )
let errors = req . validationErrors ( )
if ( errors ) {
res . render ( 'register' , {
errors : errors
} )
} else {
let newUser = new User ( {
name : name ,
email : email ,
username : username ,
password : password ,
avatar : avatar ,
role : role ,
age : age ,
state : state
} )
User . createUser ( newUser , ( err , user ) => {
if ( err ) throw err
console . log ( user )
} )
req . flash ( 'success_msg' , 'You are registered and can now login.' )
res . redirect ( '/users/login' )
}
} )
. . ....
const UserSchema = new Schema ( {
username : {
type : String ,
index : true
} ,
password : {
type : String
} ,
email : {
type : String
} ,
name : {
type : String
} ,
avatar : {
type : String
} ,
role : {
type : Number
} ,
age : {
type : Number
} ,
state : {
type : Boolean
}
} )
const User = module . exports = mongoose . model ( 'User' , UserSchema )
module . exports . createUser = function ( newUser , callback ) {
bcrypt . genSalt ( 10 , function ( err , salt ) {
bcrypt . hash ( newUser . password , salt , function ( err , hash ) {
newUser . password = hash
newUser . save ( callback )
} )
} )
}
. . ....
//Session
app . use ( session ( {
secret : 'mysecretkey' ,
saveUninitialized : true ,
resave : true
} ) )
//Passport init
app . use ( passport . initialize ( ) )
app . use ( passport . session ( ) )
//Validator
app . use ( expressValidator ( {
errorFormatter : function ( param , msg , value ) {
var namespace = param . split ( '.' ) ,
root = namespace . shift ( ) ,
formParam = root
while ( namespace . length ) {
formParam += '[' + namespace . shift ( ) + ']'
}
return {
param : formParam ,
msg : msg ,
value : value
}
}
} ) )
. . . 버그 보고서 및 끌어오기 요청은 GitHub(https://github.com/DanielArturoAlejoAlvarez/Strategy)에서 환영합니다. 이 프로젝트는 협업을 위한 안전하고 환영받는 공간이 되도록 의도되었으며 기여자는 기여자 규약 행동 강령을 준수해야 합니다.
이 gem은 MIT 라이선스 조건에 따라 오픈 소스로 제공됩니다.