-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (53 loc) · 1.75 KB
/
server.js
File metadata and controls
64 lines (53 loc) · 1.75 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
59
60
61
62
63
64
import express from "express"
import verifyToken from "./middleware.js"
import router from"./auth.js"
import pool from "./db.js"
const app = express()
app.use (express.json())
app.use("/auth", router)
app.get("/notes",verifyToken,async(req,res)=>{
try{
const notes = await pool.query("SELECT * FROM notes WHERE user_id = $1 ", [req.user.id] )
const result = notes.rows
res.json(result)
}catch(err){
console.error(err)
res.status(404).json({error:"notes not found"})
}
})
app.post("/notes",verifyToken,async(req,res)=>{
try{
const {title,note}=req.body
const notes = await pool.query("INSERT INTO notes (title,note,user_id) VALUES ($1,$2,$3) RETURNING *", [title,note, req.user.id])
const result = notes.rows[0]
res.json(result)
}catch(err){
console.error(err)
res.status(400).json({error:"The note content is empty"})
}
})
app.put("/notes/:id",verifyToken, async(req,res)=>{
try{
const {id}=req.params
const {title , note}=req.body
const notes = await pool.query("UPDATE notes SET title=$1, note=$2 WHERE id=$3 AND user_id=$4",
[title,note,id,req.user.id])
const result = notes.rows[0]
res.json(result)
}catch(err){
console.error(err)
res.status(400).json({error:"The note content is empty"})
}
})
app.delete("/notes/:id",verifyToken,async(req,res)=>{
try{
const {id}=req.params
const notes = await pool.query("DELETE FROM notes WHERE id=$1 AND user_id =$2",
[id,req.user.id])
res.json({message:"The title and the note has been deleted."})
}catch(err){
console.log(err)
res.status(404).json({error:"This note doesn't excist."})
}
})
app.listen(process.env.PORT || 8080, () => console.log("You are connected to the server."))