-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (61 loc) · 1.94 KB
/
app.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
69
70
71
72
73
74
75
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
mongoose.connect('mongodb+srv://MaiChaMH:[email protected]/DressStore', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
});
const Product = mongoose.model('Product', {
name: String,
description: String,
price: Number,
published: Boolean,
category: String,
});
app.get('/api/products', async (req, res) => {
const products = await Product.find();
res.json(products);
});
app.get('/api/products/:id', async (req, res) => {
const product = await Product.findById(req.params.id);
res.json(product);
});
app.post('/api/products', async (req, res) => {
const product = new Product(req.body);
await product.save();
res.json(product);
});
app.put('/api/products/:id', async (req, res) => {
const product = await Product.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});
res.json(product);
});
app.delete('/api/products/:id', async (req, res) => {
await Product.findByIdAndRemove(req.params.id);
res.json({ message: 'Product removed successfully' });
});
app.delete('/api/products', async (req, res) => {
await Product.deleteMany({});
res.json({ message: 'All products removed' });
});
app.get('/api/products', async (req, res) => {
const keyword = req.query.name;
const products = await Product.find({ name: { $regex: keyword, $options: 'i' } });
res.json(products);
});
app.get('/', (req, res) => {
res.json({ message: 'Welcome to the DressStore application.' });
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});