-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.js
50 lines (46 loc) · 1.35 KB
/
day19.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import express from 'express'
const app=express();
import mongoose from 'mongoose';
app.use(express.json());
let validateEmail = function(email) {
var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
console.log( re.test(email));
return re.test(email);
};
const UserSchema=mongoose.Schema({
username: String,
email: {
type: String,
trim: true,
lowercase: true,
unique: true,
validate: [validateEmail],
// match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address']
}
})
const User=mongoose.model("user",UserSchema)
function adduserwithValidation(req,res,next){
const connectionString='mongodb+srv://admin:@cluster0.jhhmwud.mongodb.net/newusers'
mongoose.connect(connectionString)
const username=req.body.username;
const email=req.body.email;
if(validateEmail(email)){
const newuser=new User({
username:username,
email:email
})
newuser.save();
next();
}
else{
res.send("some error in email check it ")
}
}
app.use(adduserwithValidation);
app.get("/users", (req,res)=>{
res.send('done sucessfully');
})
app.use((req,res,next)=>{
res.json("error ocuured!")
})
app.listen(3000);