-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (51 loc) · 1.41 KB
/
index.js
File metadata and controls
58 lines (51 loc) · 1.41 KB
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
51
52
53
54
55
56
57
58
/*
End points:
Register
Login
posts - view every post so far
Profile - View your posts, How many people you follow, how many people follow you
createPost
*/
const express = require("express");
const mongoose = require("mongoose");
const jwt = require("jsonwebtoken");
mongoose.connect("mongodb+srv://rohitlal19022006:whoosh@blogger.rzfscbw.mongodb.net/users");
const userSchema = mongoose.model('Users',{
username: String,
password: String
});
const app = express();
app.use(express.json());
app.post("/register", async (req, res)=>{
const username = req.body.username;
const password = req.body.password;
const userExists = await userSchema.findOne({
username : username
});
if(userExists){
return res.status(400).send("Username taken! Choose a different name");
};
const new_user = new userSchema({
username: username,
password: password
});
new_user.save();
res.send("New user registered");
});
app.post("/login", async (req, res)=>{
const username = req.body.username;
const password = req.body.password;
const user = await User.findOne({
username : username
});
if(!user){
return res.status(400).send("Username doesn't exist");
};
if(user.password == password){
return res.send("Yay you're in ;)");
} else{
return res.send("Wrong username/password")
}
});
app.get()
app.listen(2500);