Skip to content

Commit 957a7bf

Browse files
fix
1 parent a2ba3c4 commit 957a7bf

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

backend/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
data/
3+
npm-debug.log

backend/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /app
4+
5+
COPY package.json .
6+
RUN npm install --production
7+
8+
COPY server.js .
9+
10+
RUN mkdir -p data
11+
12+
EXPOSE 3000
13+
14+
CMD ["npm", "start"]

backend/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "workshop-survey-backend",
3+
"version": "1.0.0",
4+
"description": "Backend API para encuesta del taller Humanos Opcionales",
5+
"main": "server.js",
6+
"scripts": {
7+
"start": "node server.js"
8+
},
9+
"dependencies": {
10+
"express": "^4.18.2",
11+
"cors": "^2.8.5"
12+
}
13+
}

backend/server.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const express = require('express');
2+
const cors = require('cors');
3+
const fs = require('fs').promises;
4+
const path = require('path');
5+
6+
const app = express();
7+
const PORT = 3000;
8+
const DATA_FILE = path.join(__dirname, 'data', 'responses.json');
9+
10+
app.use(cors());
11+
app.use(express.json());
12+
13+
async function ensureDataFile() {
14+
try {
15+
await fs.mkdir(path.dirname(DATA_FILE), { recursive: true });
16+
try {
17+
await fs.access(DATA_FILE);
18+
} catch {
19+
await fs.writeFile(DATA_FILE, JSON.stringify([]));
20+
}
21+
} catch (error) {
22+
console.error('Error creating data file:', error);
23+
}
24+
}
25+
26+
async function readResponses() {
27+
try {
28+
const data = await fs.readFile(DATA_FILE, 'utf8');
29+
return JSON.parse(data);
30+
} catch (error) {
31+
console.error('Error reading responses:', error);
32+
return [];
33+
}
34+
}
35+
36+
async function writeResponses(responses) {
37+
try {
38+
await fs.writeFile(DATA_FILE, JSON.stringify(responses, null, 2));
39+
return true;
40+
} catch (error) {
41+
console.error('Error writing responses:', error);
42+
return false;
43+
}
44+
}
45+
46+
app.get('/api/responses', async (req, res) => {
47+
const responses = await readResponses();
48+
res.json(responses);
49+
});
50+
51+
app.post('/api/responses', async (req, res) => {
52+
const newResponse = {
53+
...req.body,
54+
timestamp: new Date().toISOString()
55+
};
56+
57+
const responses = await readResponses();
58+
responses.push(newResponse);
59+
60+
const success = await writeResponses(responses);
61+
62+
if (success) {
63+
res.status(201).json({ message: 'Response saved', response: newResponse });
64+
} else {
65+
res.status(500).json({ error: 'Failed to save response' });
66+
}
67+
});
68+
69+
app.get('/health', (req, res) => {
70+
res.json({ status: 'ok' });
71+
});
72+
73+
ensureDataFile().then(() => {
74+
app.listen(PORT, '0.0.0.0', () => {
75+
console.log(`Survey API running on port ${PORT}`);
76+
});
77+
});

nginx.conf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
5+
root /usr/share/nginx/html;
6+
index index.html;
7+
8+
# Servir archivos estáticos
9+
location / {
10+
try_files $uri $uri/ /index.html;
11+
}
12+
13+
# Proxy para API backend
14+
location /api/ {
15+
proxy_pass http://backend:3000/api/;
16+
proxy_http_version 1.1;
17+
proxy_set_header Upgrade $http_upgrade;
18+
proxy_set_header Connection 'upgrade';
19+
proxy_set_header Host $host;
20+
proxy_cache_bypass $http_upgrade;
21+
proxy_set_header X-Real-IP $remote_addr;
22+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
23+
proxy_set_header X-Forwarded-Proto $scheme;
24+
}
25+
}

0 commit comments

Comments
 (0)