-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (30 loc) · 956 Bytes
/
server.js
File metadata and controls
45 lines (30 loc) · 956 Bytes
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
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const port = 3000;
const menuItemSchema = new mongoose.Schema({
dish: String,
price: Number,
description: String,
}, { collection: 'menuItems' });
const MenuItem = mongoose.model('MenuItem', menuItemSchema);
mongoose.connect('mongodb://127.0.0.1:27017/restaurantDB')
.then(() => console.log('MongoDB connected…'))
.catch(err => console.log(err));
app.use(express.json());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.get('/menu', async (req, res) => {
try {
const menuItems = await MenuItem.find(); // Fetching all menu items
res.json(menuItems);
} catch (error) {
res.status(500).send({ message: error.message });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});