-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (43 loc) · 1.6 KB
/
server.js
File metadata and controls
52 lines (43 loc) · 1.6 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
const express = require('express');
const path = require('path');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const fs = require('fs');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// Session
app.use(session({
secret: 'TIN_PROJECT_SECRET',
resave: false,
saveUninitialized: false,
cookie: { httpOnly: true, maxAge: 1000 * 60 * 60 * 24 }
}));
const authRoutes = require('./routes/authAPI');
const studentRoutes = require('./routes/studentsAPI');
const courseRoutes = require('./routes/coursesAPI');
app.use('/api/auth', authRoutes);
app.use('/api/students', studentRoutes);
app.use('/api/courses', courseRoutes);
// frontend calls this to get the dictionary (e.g., /api/locales/en)
app.get('/api/locales/:lang', (req, res) => {
const lang = req.params.lang;
const allowed = ['en', 'pl'];
if (!allowed.includes(lang)) return res.status(404).json({ error: 'Language not found' });
const filePath = path.join(__dirname, 'locales', `${lang}.json`);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return res.status(500).json({ error: 'Error reading locale file' });
res.header('Content-Type', 'application/json');
res.send(data);
});
});
// catch all requests for SPA
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`SPA Server started on http://localhost:${PORT}`);
});