-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (41 loc) · 1.34 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
const express = require('express');
const cookieParser = require('cookie-parser');
const csrf = require('csurf');
const csrfProtection = csrf({ cookie: true });
const Database = require('./db/db');
const auth = require('./middleware/auth');
const errorHandler = require('./middleware/errorHandler');
const user = require('./api/user');
const post = require('./api/post');
const comment = require('./api/comment');
const app = express();
const path = require('path');
// EXPRESS SETTINGS
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static('dist'));
app.disable('x-powered-by');
// ROUTING
app.use('/api/user', user);
app.use('/api/post', post);
app.use('/api/comment', comment);
app.use(errorHandler);
// CSRF TOKEN
app.get('/api/xsrf-token', csrfProtection, auth.optional, (req, res) => {
res.send({'xsrf-token': req.csrfToken()});
});
// DEFAULT RESPONSE FOR INVALID URL
app.all('*', auth.optional, (req, res) => {
res.sendFile(path.resolve('./dist/index.html'));
});
// SQLITE
Database.init(process.env.TEST).catch((e) => {
console.log(e);
process.exit(0);
});
// EXPRESS PORT LISTEN
app.listen(process.env.PORT || 3000, () => {
console.log(`Server started succesfully at localhost:${process.env.PORT || 3000}`)
});
module.exports = app;