-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (46 loc) · 1.26 KB
/
index.js
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
// index.js
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const dotenv = require('dotenv');
dotenv.config();
const ngoSchema = new mongoose.Schema({
name: String,
description: String,
location: String,
contactInfo: String
});
const NGO = mongoose.model('NGO', ngoSchema);
module.exports = NGO;
const app = express();
app.use(cors());
app.use(express.json());
// Example route
app.get('/', (req, res) => {
res.send('Hello World!');
});
mongoose.connect(process.env.MONGO_URI)
.then(() => {
console.log('Connected to MongoDB');
})
.catch((err) => {
console.error('Failed to connect to MongoDB', err);
});
const TestSchema = new mongoose.Schema({
name: String,
});
const TestModel = mongoose.model('Test', TestSchema);
app.post('/add-test', async (req, res) => {
try {
const testDoc = new TestModel({ name: 'Test Data' });
await testDoc.save();
res.status(201).send('Document added');
} catch (err) {
res.status(500).send('Error adding document');
}
});
// Start the server
const PORT = process.env.PORT || 5001;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});