-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (49 loc) · 1.84 KB
/
server.js
File metadata and controls
61 lines (49 loc) · 1.84 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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
// Assume you have the sheetName defined
const sheetName = 'Sheet1';
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/report', async (req, res) => {
try {
const { userName, mobileNumber, crimeType, description } = req.body;
// Save the report to Google Sheets using Apps Script
const response = await fetch(
'https://script.google.com/macros/s/AKfycbyezzpLzqFKaThdEp8VqNAW_3-1u4AHmBQLe9aLdsAJm-3d46chCbRUvrcmpBSQDgYg/exec',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ userName, mobileNumber, crimeType, description }),
}
);
const data = await response.json();
// Return the complaint ID
res.json({ result: 'success', complaintId: data.complaintId });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ result: 'error', message: 'Internal server error' });
}
});
app.get('/status/:complaintId', (req, res) => {
// Implement logic to retrieve status from Google Sheets based on complaintId
const { complaintId } = req.params;
// For now, returning a sample status
const sampleStatus = 'Pending';
if (sampleStatus) {
res.json({ result: 'success', status: sampleStatus });
} else {
res.status(404).json({ result: 'error', message: 'Complaint ID not found' });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});