-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
98 lines (84 loc) · 3.16 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const express=require("express");
const app=express();
const cors= require("cors");
const pool=require("./db");
const { Ppool } =require("./dbconfig");
const session = require("express-session");
const flash = require("express-flash");
//middleware
app.use(cors());
app.use(express.json()); //req.body
app.use(express.urlencoded({extended:false}));
app.use(session({
secret:'secret',
resave:false,
saveUninitialized: false
}));
//routes
//register user
app.post("/users/register", async(req,res)=> {
//takes your form data from "localhost:5000/users/register" and save to postgreSQL database "postgres" which is under user "postgres" in table "user"
try{
const {username, email, name, bio, phoneno, password, passowrd2} = req.body; //It contains key-value pairs of data submitted in the request body by the FormData object in the 2nd parameter of fetch api which compiles the name of input and entered value as key-value pair(or we can set the JSON format using enctype="multipart/form-data" attribute of the form element) and should have match names with input name attribute of forms
const newUser =await pool.query(
"INSERT INTO users (username, email, name, bio, phoneno, password) VALUES($1,$2,$3,$4, $5, $6) RETURNING *", [username,email, name,bio, phoneno, password]
);
res.redirect('http://localhost:3006/login'); //after request redirect to this page as a response
}catch(err){
console.log(err.message);
}
})
app.post("/rooms", async(req,res)=> {
try{
const {name} = req.body;
const newUser =await pool.query(
"INSERT INTO rooms (name) VALUES($1) RETURNING *", [name]
);
res.redirect('http://localhost:3006/home');
}catch(err){
console.log(err.message);
}
})
//get all todo
app.get("/todos", async(req, res) =>{
try{
const allUsers= await pool.query("SELECT * FROM users");
res.json(allUsers.rows);
}catch(err){
console.log(err.message);
}
});
//get a todo
app.get("/todos/:id", async(req, res) => {
try{
const{id} = req.params;
const user=await pool.query("SELECT * FROM users WHERE username=$1 or name=$1 ", [id]);
res.json(user.rows);
}catch(err){
console.log(err.message);
}
});
//update a todo
app.put("/todos/:id", async(req, res) => {
try{
const{id} = req.params;
const {bio}=req.body;
const updateUser= await pool.query("UPDATE users SET bio=$1 WHERE id=$2", [bio, id])
res.json("UPDATED SUCCESSFULLY!");
}catch(err){
console.log(err.message);
}
})
//delete a todo
app.put("/todos/:id", async(req, res) => {
try{
const{username} = req.params;
const deleteUser= await pool.query("DELETE FROM users WHERE username=$1", [username]);
res.json("UPDATED SUCCESSFULLY!");
}catch(err){
console.log(err.message);
}
})
app.listen(5000, () => {
console.log("server has started");// starts the server at localhost:5000 and always start your server using nodemon index.js to keep your server running or you have to run node index.js everytime you make and save changes in index.js file
});