-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (53 loc) · 2.19 KB
/
index.js
File metadata and controls
69 lines (53 loc) · 2.19 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import path from 'path';
import config from './config.js';
import { fileURLToPath } from 'url';
import authRouter from './routes/auth.js';
import conversationRouter from './routes/conversations.js';
import messageRouter from './routes/messages.js';
import userRouter from './routes/users.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const router = express.Router();
app.use(cors());
app.use(express.json());
/* ---------------------------------------- */
/* Public directory */
/* ---------------------------------------- */
router.use(express.static(
path.resolve(path.join(__dirname, `/${config.builddir}`)))
);
/* ---------------------------------------- */
/* Pass router to subdir */
/* ---------------------------------------- */
app.use(`${config.subdir}/api`, authRouter);
app.use(`${config.subdir}/api/users`, userRouter);
app.use(`${config.subdir}/api/conversations`, conversationRouter);
app.use(`${config.subdir}/api/messages`, messageRouter);
//TODO: check router is not serving static files
app.use(config.subdir, router);
/* ---------------------------------------- */
/* Error handler middleware */
/* ---------------------------------------- */
app.use((err, _req, res, _next) => {
const statusCode = err.statusCode || 500;
console.error(err.message, err.stack);
res.status(statusCode).json({ message: err.message });
});
/* -------------------------------------------------- */
/* Catch-all route to index.html */
/* -------------------------------------------------- */
router.get('/*', (_req, res) => {
res.sendFile(path.resolve(__dirname + `/${config.builddir}/index.html`));
});
/* ---------------------------------------- */
/* Listen */
/* ---------------------------------------- */
console.log('builddir: ' + path.join(__dirname, `/${config.builddir}`));
console.log(`subdir: ${config.subdir}`);
app.listen(config.port, () => {
console.log(`Server listening on port ${config.port}`);
});