-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
115 lines (91 loc) · 3.25 KB
/
Copy pathapp.js
File metadata and controls
115 lines (91 loc) · 3.25 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const cors = require('cors')
const express = require('express')
const app = express()
const pool = require('./config/db.js')
const helmet = require('helmet')
const { noteSchema } = require('./validation.js')
const rateLimit = require('express-rate-limit')
app.set('trust proxy', 1)
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
limit: process.env.NODE_ENV === 'test' ? 10000 : 100,
standardHeaders: true,
legacyHeaders: false,
})
//middleware
app.use(helmet())
app.use(
cors({
origin: ['http://localhost:3000', 'http://localhost:5173', 'http://127.0.0.1:5500', 'https://notes-app-js.onrender.com', 'https://notes-app-jsadd.netlify.app'],
}),
)
app.use(limiter)
app.use(express.json()) //parse incoming JSON from the frontend
//Routes
app.get('/ping', (req, res) => {
res.send('ok')
})
app.post('/notes', async (req, res) => {
try {
const result = noteSchema.safeParse(req.body)
if (!result.success) {
return res.status(400).json({ error: result.error.issues[0].message })
}
const { noteTitle, noteCategory, noteContent } = result.data
const id = Date.now().toString(36)
const newNote = await pool.query('INSERT INTO notes(id, noteTitle, noteCategory, noteContent) VALUES ($1, $2, $3, $4) RETURNING *', [id, noteTitle, noteCategory, noteContent])
res.status(201).json({ message: 'Note saved!', note: newNote.rows[0] })
} catch (error) {
console.error(error.message)
}
})
app.get('/notes', async (req, res) => {
try {
const allNotes = await pool.query('SELECT * FROM notes')
res.json(allNotes.rows)
} catch (error) {
console.error(error.message)
}
})
app.get('/notes/:id', async (req, res) => {
try {
const { id } = req.params
const note = await pool.query('SELECT * FROM notes WHERE id = $1', [id])
if (note.rows.length === 0) {
return res.status(404).json({ error: 'Note not found' })
}
res.json(note.rows[0])
} catch (error) {
console.error(error.message)
}
})
app.put('/notes/:id', async (req, res) => {
try {
const { id } = req.params
const result = noteSchema.safeParse(req.body)
if (!result.success) {
return res.status(400).json({ error: result.error.issues[0].message })
}
const { noteTitle, noteCategory, noteContent } = result.data
const updateNote = await pool.query('UPDATE notes SET notetitle = $1, notecategory = $2, notecontent = $3 WHERE id = $4 RETURNING *', [noteTitle, noteCategory, noteContent, id])
if (updateNote.rows.length === 0) {
return res.status(404).json({ error: 'Note not found' })
}
res.json({ message: 'Note updated!', note: updateNote.rows[0] })
} catch (error) {
console.error(error.message)
}
})
app.delete('/notes/:id', async (req, res) => {
try {
const { id } = req.params
const note = await pool.query('DELETE FROM notes WHERE id = $1', [id])
if (note.rowCount === 0) {
return res.status(404).json({ error: 'Note not found' })
}
res.json({ sucess: true, data: note })
} catch (error) {
console.error(error.message)
}
})
module.exports = app