-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (39 loc) · 1.08 KB
/
server.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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const port = 4001;
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Connect to MongoDB
mongoose.connect(
"mongodb+srv://adnaanjanees0:cK40LtP3g3oywKjq@cluster0.1s0ub.mongodb.net/",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
// Define a schema and model
const responseSchema = new mongoose.Schema({
name: String,
value: Number,
index: Number,
});
const Response = mongoose.model("Response", responseSchema);
// API endpoint to save response
app.post("/saveResponse", async (req, res) => {
const { name, value, index } = req.body;
try {
const newResponse = new Response({ name, value, index });
await newResponse.save();
res.status(200).send("Response saved successfully!");
} catch (error) {
res.status(500).send("Error saving response");
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});