-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
68 lines (49 loc) · 1.68 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import express from 'express';
import path from 'path';
import dotenv from 'dotenv';
import cookieSession from 'cookie-session';
import cookieParser from 'cookie-parser';
import crypto from 'crypto';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { connectDB } from './config/db.js';
import { userRoutes } from './routes/userRoutes.js';
import isAuthenticated from './middleware/authMiddleware.js';
dotenv.config();
connectDB();
const app = express();
const port = process.env.PORT || 8080;
app.use(express.json());
/* ----- session ----- */
app.use(cookieSession({
name: 'session',
keys: [crypto.randomBytes(32).toString('hex')],
// Cookie Options
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}));
app.use(cookieParser());
// ...
const currentModuleUrl = new URL(import.meta.url);
const currentModulePath = dirname(fileURLToPath(currentModuleUrl));
const staticDirPath = join(currentModulePath, '.', 'static');
app.use(express.static(staticDirPath));
const publicDirPath = join(currentModulePath, '.', 'public');
app.get('/', function (req, res) {
res.sendFile(join(publicDirPath, 'index.html'));
});
app.get('/auth', function (req, res) {
res.sendFile(join(publicDirPath, 'auth.html'));
});
app.get('/user', isAuthenticated, function (req, res) {
res.sendFile(join(publicDirPath, 'user.html'));
});
app.get('/app', isAuthenticated, function (req, res) {
res.sendFile(join(publicDirPath, 'dataviz.html'));
});
app.get('/chat', isAuthenticated, function (req, res) {
res.sendFile(join(publicDirPath, 'chat.html'));
});
// ...
app.use('/api', userRoutes);
app.listen(port);
console.log('Server started at http://localhost:' + port);