-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (48 loc) · 1.29 KB
/
index.js
File metadata and controls
56 lines (48 loc) · 1.29 KB
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
const express = require('express');
const mongoose = require('mongoose');
const app = express();
app.use(express.json());
// MongoDB bağlantısı
mongoose.connect('mongodb://mongodb:27017/eticaret')
.then(() => console.log('MongoDB bağlandı'))
.catch(err => console.log('MongoDB hatası:', err));
// Basit Product schema
const Product = mongoose.model('Product', {
name: String,
price: Number,
stock: Number
});
// Ana sayfa
app.get('/', (req, res) => {
res.json({
message: '🛒 E-Ticaret API',
endpoints: ['/products', '/health']
});
});
// Sağlık kontrolü
app.get('/health', (req, res) => {
res.json({ status: 'OK', time: new Date() });
});
// Ürünleri listele
app.get('/products', async (req, res) => {
try {
const products = await Product.find();
res.json(products);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Ürün ekle
app.post('/products', async (req, res) => {
try {
const product = new Product(req.body);
await product.save();
res.status(201).json(product);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`🚀 API çalışıyor: http://localhost:${PORT}`);
}); console.log("API guncellemesi");