aboutsummaryrefslogtreecommitdiff
path: root/src/server.js
blob: 4a2a58189f560f927f0f153e6e830ba3d1ebf21d (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
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
const express = require('express');
const request = require('request');

const port = 80;

const server = express();

function listen(){
    server.listen(port, () => console.info(`Listening on port ${port}!`));
}

function setUpRoutes(models){
    server.get('/', (req, res) => res.sendFile(__dirname + "/html/index.html"))
    server.get('/bread', (req, res) => res.sendFile(__dirname + "/html/bread.html"));
    server.get('/essay', (req, res) => res.sendFile(__dirname + "/html/essay.html"));
    server.get('/snake', (req, res) => res.sendFile(__dirname + "/html/snake.html"));
    server.get('/setScore', (req, res) => {
        request(`http://localhost:8000?${req.url.split("?")[1]}`, function(error, response, body) {
        });
    })
    server.get('/:type/posts.json', async (req, res, next) =>  {
        try {
            const { type } = req.params;
            console.log(type);
            console.log(req.params);
            var posts = await models.posts.findAll({where: { type: type }});
            posts = posts.map(x => x.get({ plain: true }));            
            for (const post of posts) {
              const images = await models.pictures.findAll({ attributes: ["source"], where: { postId: post.id } }).map(x => x.source);
              post.images = images;
            }
            res.status(200).send({ success: true, data: posts });
            next();
        } catch (e) {
            res.status(400).send({ success: false, error: e.message });
        }
    })


    server.get('/favicon.ico', (req, res) => res.sendFile(__dirname + "/icon/favicon.ico"))
    server.get('/css/:id', (req, res) => {
        res.sendFile(__dirname + "/css/"+req.params.id);
    });
    server.get('/photo/:id', (req, res) => { 
        res.sendFile(__dirname + "/photo/"+req.params.id);
    });
    server.get('/essay/:id', (req, res) => { 
        res.sendFile(__dirname + "/html/essay/"+req.params.id);
    });

}

module.exports = {
    listen,
    setUpRoutes
};