-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (42 loc) · 1.26 KB
/
server.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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
// Initialize app
const app = express();
// Middleware
app.use(bodyParser.json());
// MongoDB connection
const DB_URI = 'mongodb://localhost:27017/food_delivery_backend'; // Replace with your MongoDB URI
mongoose.connect(DB_URI)
.then(() => console.log('MongoDB connected...'))
.catch(err => console.log('Database connection error:', err));
// Define a simple schema and model
const OrderSchema = new mongoose.Schema({
name: String,
status: String
});
const Order = mongoose.model('Order', OrderSchema);
// Routes
app.get('/orders', async (req, res) => {
try {
const orders = await Order.find();
res.json(orders);
} catch (err) {
res.status(400).send('Error fetching orders');
}
});
app.post('/orders', async (req, res) => {
const { name, status } = req.body;
const newOrder = new Order({ name, status });
try {
await newOrder.save();
res.status(201).json(newOrder);
} catch (err) {
res.status(400).send('Error saving order');
}
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});