forked from velosergio/PhotoWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (34 loc) · 1.07 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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3000;
// Configurar almacenamiento de Multer
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage });
app.use(express.static('public'));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Ruta para cargar imágenes
app.post('/upload', upload.single('image'), (req, res) => {
res.send('Imagen subida con éxito');
});
// Ruta para obtener imágenes
app.get('/images', (req, res) => {
fs.readdir('uploads/', (err, files) => {
if (err) {
return res.status(500).json({ error: 'No se pudieron cargar las imágenes' });
}
res.json(files);
});
});
app.listen(PORT, () => {
console.log(`Servidor corriendo en http://localhost:${PORT}`);
});