-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (59 loc) · 2.52 KB
/
Copy pathserver.js
File metadata and controls
71 lines (59 loc) · 2.52 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express');
const fs = require('fs');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
// الميدلويرات
app.use(express.json({ limit: '1000mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// حماية بعض الملفات
app.get(['/admin.html', '/server.js', '/package.json'], (req, res) => {
const filePath = path.join(__dirname, req.path);
return fs.existsSync(filePath)
? res.sendFile(filePath)
: res.status(404).send('404 Not Found');
});
// حفظ البيانات
app.post('/api/save', (req, res) => saveJson('data.json', req, res));
app.post('/api/save2', (req, res) => saveJson('data2.json', req, res));
app.post('/api/savemm', (req, res) => saveJson('mm.json', req, res)); // حفظ بيانات mm.json
app.post('/api/savedd', (req, res) => saveJson('dd.json', req, res)); // حفظ بيانات dd.json
function saveJson(file, req, res) {
try {
fs.writeFileSync(file, JSON.stringify(req.body, null, 2), 'utf8');
res.json({ success: true, message: `تم الحفظ في ${file}` });
} catch (err) {
res.status(500).json({ error: `فشل في حفظ ${file}` });
}
}
// قراءة البيانات
app.get('/api/data', (req, res) => sendJson('data.json', res));
app.get('/api/2data', (req, res) => sendJson('data2.json', res));
app.get('/api/mm', (req, res) => sendJson('mm.json', res)); // قراءة بيانات mm.json
app.get('/api/dd', (req, res) => sendJson('dd.json', res)); // قراءة بيانات dd.json
function sendJson(file, res) {
try {
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
res.json(data);
} catch (err) {
res.status(500).json({ error: `فشل في قراءة ${file}` });
}
}
// منع الوصول المباشر للملفات الحساسة
app.get('/data.json', (_, res) => res.status(404).json({ error: 'not found' }));
app.get('/data2.json', (_, res) => res.status(404).json({ error: 'not found' }));
app.get('/mm.json', (_, res) => res.status(404).json({ error: 'not found' }));
app.get('/dd.json', (_, res) => res.status(404).json({ error: 'not found' }));
// تقديم الملفات الثابتة
app.use(express.static(__dirname));
// الصفحة الرئيسية
app.get('/', (_, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// تشغيل الخادم
app.listen(port, () =>
console.log(`✅ الخادم يعمل على http://localhost:${port}`)
);