-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_book.js
33 lines (28 loc) · 1 KB
/
server_book.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
const express = require('express');
const { MongoClient } = require('mongodb');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(cors());
const url = 'mongodb://localhost:27017';
const dbName = 'mydatabase';
async function connectToDatabase() {
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });
await client.connect();
return client.db(dbName);
}
app.post('/register', async (req, res) => {
try {
const { Passengers, Depart, Arrive, Name, Phone, Altph,Email,Choice } = req.body;
const db = await connectToDatabase();
await db.collection('books').insertOne({ Passengers, Depart, Arrive, Name, Phone, Altph,Email,Choice });
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: 'Internal Server Error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});