-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
44 lines (37 loc) · 1.17 KB
/
app.js
File metadata and controls
44 lines (37 loc) · 1.17 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
const express = require('express');
const { MongoClient, ServerApiVersion } = require('mongodb');
const bodyParser = require('body-parser');
const Website = require('./models/websiteModel');
const app = express();
const PORT = 3000;
// Middleware
app.use(bodyParser.json());
// MongoDB connection
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
// API endpoint to create a new website entry
app.post('/api/websites', async (req, res) => {
try {
await client.connect();
const database = client.db("DarkPatterns");
const collection = database.collection("websites");
const { "website-url": websiteUrl, "website-info": websiteInfo } = req.body;
const newWebsite = { "website-url": websiteUrl, "website-info": websiteInfo };
const result = await collection.insertOne(newWebsite);
res.json(result.ops[0]);
} catch (error) {
res.status(500).json({ error: error.message });
} finally {
await client.close();
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});