-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (45 loc) · 1.34 KB
/
server.js
File metadata and controls
54 lines (45 loc) · 1.34 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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const path = require("path");
const app = express();
app.use(cors());
app.use(express.json());
// serve static files (like Form.html, CSS, JS)
app.use(express.static(__dirname));
// MongoDB connection
const mongoURI = process.env.MONGODB_URI;
mongoose.connect(mongoURI);
// Schema
const responseSchema = new mongoose.Schema({
username: String,
password: String,
gender: String,
age: String,
about: String,
lifestyle: String,
status: String, // NEW → How have you been lately?
mood: String,
thoughts: String, // NEW → Reflection
stress: String, // NEW → What's causing stress?
coping: String, // NEW → How handling stress
worries: String,
extra: String, // NEW → Anything else
consent: Boolean, // NEW → Consent checkbox
});
const Response = mongoose.model("Response", responseSchema);
// API endpoint
app.post("/api/responses", async (req, res) => {
try {
const response = new Response(req.body);
await response.save();
res.status(201).send("Saved successfully");
} catch (error) {
res.status(500).send("Error saving response");
}
});
// use Render's port
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});