Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (63 loc) · 1.97 KB
/
Copy pathindex.js
File metadata and controls
72 lines (63 loc) · 1.97 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const port = 3000;
app.use(express.json());
app.use(cors());
mongoose.connect('mongodb://localhost:27017/fmp', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch(err => {
console.error('Failed to connect to MongoDB', err);
process.exit(1); // Exit the process if the connection fails
});
const featureFlagSchema = new mongoose.Schema({
name: String,
enabled: Boolean
});
const FeatureFlag = mongoose.model('FeatureFlag', featureFlagSchema);
app.get('/feature/:name', async (req, res) => {
const feature = await FeatureFlag.findOne({ name: req.params.name });
if (feature) {
res.json({ enabled: feature.enabled });
} else {
res.status(404).send('Feature not found');
}
});
// Create a new feature flag
app.post('/feature', async (req, res) => {
const { name, enabled } = req.body;
if (!name || typeof enabled !== 'boolean') {
return res.status(400).send('Invalid input');
}
const feature = new FeatureFlag({ name, enabled });
await feature.save();
res.status(201).send('Feature flag created');
});
// Update an existing feature flag
app.put('/feature/:name', async (req, res) => {
const { enabled } = req.body;
if (typeof enabled !== 'boolean') {
return res.status(400).send('Invalid input');
}
const feature = await FeatureFlag.findOneAndUpdate(
{ name: req.params.name },
{ enabled },
{ new: true }
);
if (feature) {
res.send('Feature flag updated');
} else {
res.status(404).send('Feature not found');
}
});
// Delete a feature flag
app.delete('/feature/:name', async (req, res) => {
const feature = await FeatureFlag.findOneAndDelete({ name: req.params.name });
if (feature) {
res.send('Feature flag deleted');
} else {
res.status(404).send('Feature not found');
}
});
module.exports = app; // Export the app instance