-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (39 loc) · 1.3 KB
/
server.js
File metadata and controls
50 lines (39 loc) · 1.3 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 9000;
// Configure CORS
const corsOptions = {
origin: 'http://localhost:7980', // Allow only the frontend application
// Can add more domains
// origin: ['http://localhost:7980', 'domain2', 'domain3'],
optionsSuccessStatus: 200 // For legacy browser support
};
app.use(cors(corsOptions));
app.use(express.json());
mongoose.connect('mongodb://localhost/stickynotesdb', { useNewUrlParser: true, useUnifiedTopology: true });
const NoteSchema = new mongoose.Schema({
header: String,
text: String,
color: String
});
const Note = mongoose.model('Note', NoteSchema);
app.get('/api/notes', async (req, res) => {
const notes = await Note.find();
res.json(notes);
});
app.post('/api/notes', async (req, res) => {
const note = new Note(req.body);
await note.save();
res.json(note);
});
app.put('/api/notes/:id', async (req, res) => {
const note = await Note.findByIdAndUpdate(req.params.id, req.body, { new: true });
res.json(note);
});
app.delete('/api/notes/:id', async (req, res) => {
await Note.findByIdAndDelete(req.params.id);
res.json({ message: 'Note deleted' });
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));