-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (41 loc) · 1.29 KB
/
server.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
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const PORT = 3007;
// Serve uploaded files as static assets
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Configure multer for file uploads
const upload = multer({
dest: 'uploads/', // Folder to store uploaded files
limits: { fileSize: 5 * 1024 * 1024 }, // Max file size: 5 MB
});
// Create an endpoint to upload images
app.post('/upload', upload.single('image'), (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
// Log uploaded file details
console.log('Uploaded File Details:', req.file);
const { originalname, mimetype, size, path: savedPath } = req.file;
// Send back the file URL
const fileUrl = `${req.protocol}://${req.get('host')}/uploads/${req.file.filename}`;
res.status(200).json({
message: 'Image uploaded successfully!',
fileDetails: {
originalName: originalname,
mimeType: mimetype,
size: size,
savedPath: savedPath,
fileUrl: fileUrl,
},
});
});
// Test API
app.get('/', (req, res) => {
res.send('Local API is running...');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});