Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (57 loc) · 2 KB
/
Copy pathserver.js
File metadata and controls
65 lines (57 loc) · 2 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
const express = require('express');
const multer = require('multer');
const path = require('path');
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const dotenv = require('dotenv');
const cors = require('cors');
// Load environment variables
dotenv.config();
// Create Express app
const app = express();
const PORT = process.env.PORT || 3000;
// Configure CORS - only allow requests from your domain
const allowedOrigins = [
'http://localhost:3000', // Local development
'https://yourdomain.com', // Production site
'https://your-username.github.io' // GitHub Pages
];
const corsOptions = {
origin: function (origin, callback) {
// During development, origin might be undefined (e.g., from Postman)
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type', 'Authorization']
};
app.use(cors(corsOptions));
// Serve static files from 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Configure file upload
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const uploadDir = path.join(__dirname, 'uploads');
// Ensure directory exists
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
cb(null, uploadDir);
},
filename: function (req, file, cb) {
// Create unique filename with timestamp
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, uniqueSuffix + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage,
limits: { fileSize: 5 * 1024 * 1024 }, // Limit to 5MB
fileFilter: function (req, file, cb) {
// Accept only image files
if (!file.mimetype.startsWith('image/')) {
return cb(new Error('Only image