-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (56 loc) · 1.99 KB
/
Copy pathserver.js
File metadata and controls
71 lines (56 loc) · 1.99 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
import express from 'express';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import apiRouter from './routes/api.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = 3000;
// Middleware for parsing JSON and urlencoded requests
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Ensure uploads directory exists
const uploadsDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
// Log incoming requests for debugging & activity
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
next();
});
// Middleware to serve `/javascript` with the correct Content-Type header
app.get('/javascript', (req, res) => {
res.setHeader('Content-Type', 'application/javascript');
res.sendFile(path.join(__dirname, 'javascript'));
});
// Serve uploads folder statically
app.use('/uploads', express.static(uploadsDir));
// Connect API routes
app.use('/api', apiRouter);
// Serve static files from root directory
app.use(express.static(__dirname));
// Custom HTML routes to ensure seamless page loads
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/products.html', (req, res) => {
res.sendFile(path.join(__dirname, 'products.html'));
});
app.get('/order.html', (req, res) => {
res.sendFile(path.join(__dirname, 'order.html'));
});
app.get('/admin.html', (req, res) => {
res.sendFile(path.join(__dirname, 'admin.html'));
});
// Global Error Handler
app.use((err, req, res, next) => {
console.error('Server error boundary:', err);
res.status(err.status || 500).json({
error: err.message || 'حدث خطأ غير متوقع في خادم المتجر الرئيسي'
});
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port ${PORT} at host 0.0.0.0`);
});