-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.js
68 lines (49 loc) · 1.77 KB
/
save.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require('express');
const multer = require('multer');
const MongoClient = require('mongodb').MongoClient;
const port = 8000;
// Connection URL and database name
const url = 'mongodb://localhost:27017';
const dbName = 'hideout';
// Create Express app
const app = express();
// Set up multer for handling file uploads
const upload = multer({ dest: 'uploads/' });
// Set static folder for serving HTML, CSS, and JavaScript files
app.use(express.static('public'));
// Define route for form submission
app.post('/upload', upload.single('photoInput'), async (req, res) => {
// Get input values from request body
const { placeNameInput, experienceInput } = req.body;
const photoUrl = req.file.path; // path to the uploaded photo
try {
// Create a new MongoClient
const client = new MongoClient(url);
// Connect to the MongoDB server
await client.connect();
// Select the database
const db = client.db(hideout);
// Create a new collection (if necessary) or use an existing one
const collection = db.collection('queries');
// Create a document with user data
const userData = {
photoUrl,
placeName: placeNameInput,
experience: experienceInput
};
// Insert the document into the collection
const result = await collection.insertOne(userData);
// Log the inserted document ID
console.log('User data saved with ID:', result.insertedId);
res.sendStatus(200); // Send success response
} catch (error) {
console.error('Error saving user data:', error);
res.sendStatus(500); // Send error response
} finally {
client.close(); // Close the MongoDB connection
}
});
// Start the server
app.listen(port, () => {
console.log(`The application started successfully on port ${port}`);
});