-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (52 loc) · 2.31 KB
/
server.js
File metadata and controls
65 lines (52 loc) · 2.31 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
// Import necessary libraries
const express = require('express');
const admin = require('firebase-admin');
// --- FIREBASE SETUP ---
// You MUST replace 'path/to/your/serviceAccountKey.json' with the
// actual path to your downloaded service account key file.
// IMPORTANT: This file should be kept private and never committed to version control.
const serviceAccount = require('./serviceAccountKey.json');
// Initialize the Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
// Get a reference to the Firestore database
const db = admin.firestore();
// Create an Express application
const app = express();
const port = 3000;
// Use built-in middleware to parse incoming JSON requests
app.use(express.json());
// Set up a simple CORS (Cross-Origin Resource Sharing) policy
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
// This is the API endpoint that will handle your form submissions.
app.post('/submit-form', async (req, res) => {
try {
const formData = req.body;
// Log the received data to the server console
console.log('Received form data:');
console.log(formData);
// Save the form data to a Firestore collection
// The collection name is 'formSubmissions'. You can change this.
const collectionRef = db.collection('formSubmissions');
// Add a new document to the collection with the form data
const docRef = await collectionRef.add(formData);
// Send a success response back to the client with the new document ID
console.log(`Document written with ID: ${docRef.id}`);
res.status(200).json({ message: 'Form submitted successfully!', docId: docRef.id });
} catch (error) {
// Handle any errors that occur during processing or Firestore operations
console.error('Error processing form submission:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
});
// Start the server and make it listen for incoming requests
app.listen(port, () => {
console.log(`Backend API listening at http://localhost:${port}`);
console.log('Waiting for form submissions...');
});