-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
executable file
·69 lines (52 loc) · 1.87 KB
/
app.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
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const session = require('express-session');
const schedule = require('node-schedule');
const sequelize = require('./src/config/database');
const fs = require('fs');
const sessionConfig = require('./src/config/session');
//models
const Meme = require('./src/models/meme');
const Vote = require('./src/models/vote');
//routes
const mainRoutes = require('./src/routes/index');
const authRoutes = require('./src/routes/auth');
const registerRoutes = require('./src/routes/register');
const apiRoutes = require('./src/routes/api');
const addMemeRoutes = require('./src/routes/addMeme');
const memeApiRoutes = require('./src/routes/memeApi');
const voteRoutes = require('./src/routes/vote');
const profileRoutes = require('./src/routes/profile');
app.use(`/dist`, express.static(__dirname + "/dist"));
app.use(`/static`, express.static(__dirname + "/static"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session(sessionConfig));
app.use(`/`, mainRoutes);
app.use(`/auth`, authRoutes);
app.use(`/register`, registerRoutes);
app.use(`/add`, addMemeRoutes);
app.use(`/profile`, profileRoutes);
app.use(`/api`, apiRoutes);
app.use(`/api`, memeApiRoutes);
app.use(`/api/vote`, voteRoutes);
//job which every minute deletes meme which is older than 1 hour.
schedule.scheduleJob({rule: '*/60 * * * * *'}, async () => {
const memes = await Meme.findAll({
where: sequelize.literal(`created_at < now() - interval '1 hour'`)
});
for(let meme of memes){
fs.unlink(meme.src, err => { if(err) throw err })
await Meme.destroy({
where: {
src: meme.src
}
});
}
});
app.listen(8081, () => {
console.log('Hello! MEMEnto-mori app listening on port 8081');
});
module.exports = app;