-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
60 lines (52 loc) · 1.77 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
'use strict';
const bodyParser = require('body-parser');
const express = require('express');
const expressValidator = require('express-validator');
const helmet = require('helmet');
const logger = require('morgan');
const mongoose = require('mongoose');
const config = require('config');
const authenticator = require('./middlewares/authenticator');
const errorHandler = require('./middlewares/errorHandler');
const routes = require('./routes');
const cors = require('cors');
const path = require('path');
const _ = require('lodash');
global.basePath = path.resolve(__dirname);
const app = express();
app.set('view engine', 'ejs');
mongoose.connect(`mongodb://${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`);
app.use('/media', express.static(path.join(__dirname, 'media')));
app.use(logger('dev'));
app.use(helmet());
app.use(helmet.noCache());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false, limit: 5 * 1024 * 1024 }));
app.use(bodyParser.json({ limit: 5 * 1024 * 1024 }));
app.use(expressValidator({
customValidators: {},
}));
app.use(authenticator.authenticate());
app.use(express.static(__dirname + '/public'));
const Message = require('./models/message');
const User = require('./models/user');
app.get('/', (req, res) => {
Promise.all([
User.find().limit(300).select('profileImage'),
Message.find().limit(30).populate('user').sort({ _id: -1 })
])
.then(values => {
res.render('index', {
thumbnails: _.map(values[0], 'profileImage'),
messages: values[1],
next: values[1].length ? _.last(values[1])._id : null
});
})
.catch(err => {
res.render('index');
});
});
app.use('/users', routes.users);
app.use('/messages', routes.messages);
app.use(errorHandler());
module.exports = app;