-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (70 loc) · 2.05 KB
/
Copy pathapp.js
File metadata and controls
84 lines (70 loc) · 2.05 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import express from 'express';
import session from 'express-session';
import mongoose from 'mongoose';
import cors from 'cors';
import url from 'url';
import i18next from 'i18next';
import i18nextMiddleware from 'i18next-express-middleware';
import Backend from 'i18next-node-fs-backend';
import config from './config/config';
import User from './models/user'; // eslint-disable-line no-unused-vars
import passport from './config/passport'; // eslint-disable-line no-unused-vars
import router from './routes/index';
const app = express();
// Session config
app.use(session({
secret: config.PASS.SECRET,
cookie: { maxAge: 60000 },
resave: false,
rolling: true,
saveUninitialized: false,
}));
// db url
const mongoDB = process.env.MONGOLAB_URI || config.PASS.DB.url;
// db connect
mongoose.connect(mongoDB, {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
});
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// CORS
if (config.CORS_ENABLED) {
const corsOptions = {
credentials: true, // eslint-disable-line object-shorthand
origin: (origin, callback) => {
callback(null, origin);
},
};
app.use(cors(corsOptions));
}
// Static
if (config.STATIC_SERVE) {
const mediaURL = new url.URL(config.MEDIA_URL);
app.use(mediaURL.pathname, express.static(config.MEDIA_DIR));
}
i18next
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.init({
backend: {
// eslint-disable-next-line prefer-template, no-path-concat
loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json',
// eslint-disable-next-line prefer-template, no-path-concat
addPath: __dirname + '/locales/{{lng}}/{{ns}}.missing.json',
},
fallbackLng: 'en',
preload: ['en', 'ru'],
saveMissing: true,
detection: {
order: ['cookie'],
lookupCookie: 'language',
},
});
app.use(i18nextMiddleware.handle(i18next));
app.use(passport.initialize());
app.use(passport.session());
app.use(router);
export default app;