summaryrefslogtreecommitdiff
path: root/routes.js
blob: 012befe7c2217cdce7d319c0e5c405afd8138b89 (plain)
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
const express = require('express');
const path = require('path');
const Url = require('./db/url-schema.js');
const utils = require('./utils.js');
const router = express.Router();

router.get('/', async (req, res) => {
    utils.sendFile(res, 'static/index.html');
});

router.get('/:slug', async (req, res) => {
    try {
        const url = await Url.findOne({slug: req.params.slug.toLowerCase()});
        if (url)
            res.redirect(301, url.url);
        else
            utils.sendNotFoundError(res);
    } catch (err) {
        utils.sendInternalServerError(res);
    }
});

router.all('/*', async (req, res) => {
    utils.sendNotFoundError(res);
});

module.exports = router;