-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (88 loc) · 2.98 KB
/
Copy pathserver.js
File metadata and controls
101 lines (88 loc) · 2.98 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
// import Express library and activate it
import express from "express";
const app = express();
// import path module to help with file paths
import path from 'path';
// Serve static files from the 'public' folder
app.use(express.static('public'))
// On Vercel, point the root url (/) to index.html explicitly
if (process.env.VERCEL) {
app.get('/', (req, res) => {
res.sendFile(path.join(process.cwd(), 'public', 'index.html'))
})
}
import cors from 'cors'
app.use(cors())
app.use(express.json())
import { Story } from "./models/StoryModel.js"
import { mongoReady } from './database.js'
/** Endpoint for fetching nearby stories from MongoDB */
// the user's location is passed along via query parameters
app.get('/api/stories', mongoReady, async (req, res) => {
// Query mongoDB for nearby stories using the $near operator
// see also: https://www.mongodb.com/docs/manual/reference/operator/query/near/
let stories = await Story.find({
"location": {
$near: {
$geometry: {
type: "Point",
coordinates: [req.query.lng, req.query.lat]
},
$maxDistance: 1000000
}
}
}).limit(10)
res.send(stories)
})
/* This endpoint is for adding a new story.
The frontend sends lat/lng coordinates and Content/Text as JSON in the body of the request.
We parse this JSON using the "bodyParser" middleware and save it to MongoDB.
see also: https://www.npmjs.com/package/body-parser */
app.post('/api/story', mongoReady, (req, res) => {
/* todo: insert new story into mongo */
let story = new Story({
"content": req.body.content,
"location": req.body.location
})
story.save().then((status) => {
res.send({
"status": status
})
})
})
/* Delete endpoint for community moderation
Anyone can remove offensive posts */
app.delete('/api/story/:id', mongoReady, async (req, res) => {
try {
const result = await Story.findByIdAndDelete(req.params.id)
if (result) {
res.send({ status: 'deleted', id: req.params.id })
} else {
res.status(404).send({ error: 'Story not found' })
}
} catch (err) {
res.status(500).send({ error: 'Failed to delete story' })
}
})
/* Update endpoint for editing story content */
app.put('/api/story/:id', mongoReady, async (req, res) => {
try {
const result = await Story.findByIdAndUpdate(
req.params.id,
{ content: req.body.content },
{ new: true } // return the updated document
)
if (result) {
res.send({ status: 'updated', story: result })
} else {
res.status(404).send({ error: 'Story not found' })
}
} catch (err) {
res.status(500).send({ error: 'Failed to update story' })
}
})
const port = 3000
// Start Express
app.listen(port, () => {
console.log(`Express is Live at http://localhost:${port}`);
});