aboutsummaryrefslogtreecommitdiffstats
path: root/src/server.js
blob: 9fe2c4b00fdf17ebd078e36ec138c5fb21ef2010 (plain) (blame)
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
import express from "express";
import path from 'path';
import { fileURLToPath } from 'url';

import db from './db.js';
import usersRouter from './routes/users.js';
import messagesRouter from './routes/messages.js';
import roomsRouter from './routes/rooms.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const DEFAULT_PORT = 3000;
const PORT = process.env.PORT || DEFAULT_PORT;

const app = express();

app.use(express.json());

function serveDocumentation(req, res) {
    const filePath = path.join(__dirname, '../views/index.html');
    res.sendFile(filePath);
}

function onServerStart() {
    console.log('Listening on port ' + PORT);
}

async function start() {
    await db.connect();
    app.listen(PORT, onServerStart);
}

app.get('/', serveDocumentation);
app.use('/api/users', usersRouter);
app.use('/api/messages', messagesRouter);
app.use('/api/rooms', roomsRouter);

start();