Commit 8de9704a authored by “Yevgeniy's avatar “Yevgeniy

#6 added packages

parent 4071d664
This diff is collapsed.
...@@ -20,5 +20,15 @@ ...@@ -20,5 +20,15 @@
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^4.8.4" "typescript": "^4.8.4"
},
"dependencies": {
"@types/bcrypt": "^5.0.0",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"bcrypt": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"mongoose": "^6.7.0",
"nanoid": "^3.3.4"
} }
} }
import { Schema, model } from 'mongoose';
import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid';
const SALT_WORK_FACTOR= 10;
// 1. Create an interface representing a document in MongoDB.
interface IUser {
name: string;
surname: string;
email: string;
displayName: string;
password:string;
role: string;
}
// 2. Create a Schema corresponding to the document interface.
const UserSchema = new Schema<IUser>({
name: { type: String, required: true },
surname: { type: String, required: true },
email: { type: String, required: true },
displayName: { type: String, required: true },
password: { type: String, required: true },
role:{type:String,default: 'user',enum:['user','admin']}
});
// How does next's type defined?
UserSchema.pre('save', async function(next:any):Promise<void>{
if(!this.isModified('password')) return next();
console.log('next', next)
const salt = await bcrypt.genSalt(SALT_WORK_FACTOR);
const hash = await bcrypt.hash(this.password, salt);
this.password = hash;
next();
})
UserSchema.set('toJSON',{
transform:(doc:any, ret:any, options:any)=>{
delete ret.password;
return ret;
}
})
UserSchema.methods.checkPassword =function(password:string):Promise<boolean>{
return bcrypt.compare(password,this.password);
}
UserSchema.methods.generateToken =function(){
this.token=nanoid();
}
// 3. Create a Model.
const User = model<IUser>('User', UserSchema);
export default User;
import express,{Router, Request, Response} from 'express';
import User from '../models/User';
const router:Router = express.Router();
router.post('/', async (req : Request, res : Response) => {
try{
const {name,surname,password,email} = req.body;
const displayName = surname+' '+name[0]+'.'
const user = new User({
name:name,
surname:surname,
email:email,
password:password,
displayName:displayName
})
await user.save();
return res.send({user})
}catch(e ){
if (e instanceof Error){
return res.status(404).send({'message':e.message});
}
return res.status(500).send({'message':'Broke server'});
}
})
router.get('/', async (req : Request, res : Response):Promise<Response<any, Record<string, any>>> => {
const users = await User.find()
return res.send({users})
})
export default router;
console.log('Hello Michail') import express, { Express } from 'express';
\ No newline at end of file import cors from 'cors';
import mongoose from 'mongoose';
import users from './routers/users';
console.log('Hello world!');
const app:Express = express();
app.use(express.static('public'));
app.use(cors())
app.use(express.json());
const PORT = 8000;
app.use('/users',users)
const run = async() => {
mongoose.connect(`mongodb://localhost:27017/task-planner`);
app.listen(PORT, () => {
console.log(`Server started at http://localhost:${PORT}/`);
})
process.on("exit", () => {
mongoose.disconnect();
})
}
run().catch(console.error);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment