-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (50 loc) · 1.74 KB
/
Copy pathserver.js
File metadata and controls
59 lines (50 loc) · 1.74 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
const express = require('express');
const { MongoClient } = require('mongodb');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const port = 3000;
// Middleware
app.use(bodyParser.json());
app.use(cors()); // Enable CORS for all origins (consider tightening this for production)
// MongoDB connection URI
const uri = 'mongodb+srv://theogriffinjones:po0rtBJe4c7eromw@trashai.ksdqmv9.mongodb.net/'
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// Connect to MongoDB
async function connectToMongoDB() {
try {
await client.connect();
console.log('Connected to MongoDB');
} catch (error) {
console.error('Could not connect to MongoDB', error);
process.exit(1);
}
}
connectToMongoDB();
// Assuming you have a "trashBins" collection in your MongoDB
const db = client.db('trashAI');
const collection = db.collection('trashAI');
// Routes
// Get all trash bin locations
app.get('/get', async (req, res) => {
try {
const trashBins = await collection.find({}).toArray();
res.status(200).json(trashBins);
} catch (error) {
res.status(500).json({ message: 'Failed to get trash bins', error });
}
});
// Add a new trash bin location
app.post('/post', async (req, res) => {
try {
const { name, latitude, longitude } = req.body;
const result = await collection.insertOne({ name, latitude, longitude });
res.status(201).json({ message: 'Trash bin added', result });
} catch (error) {
res.status(500).json({ message: 'Failed to add trash bin', error });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});