-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathapp.js
56 lines (47 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
49
50
51
52
53
54
55
56
import bodyParser from 'body-parser'
import chalk from 'chalk'
import cors from 'cors'
import dotenv from 'dotenv'
import express from 'express'
import i18n from 'i18n'
import mongoose from 'mongoose'
import path from 'path'
import { startRedis } from './controllers/rateLimit'
import appRoutes from './routes'
import './models/book'
import './models/verse'
import './models/request'
dotenv.config()
startRedis()
const app = express()
// Connect to MongoDB.
mongoose.set('useCreateIndex', true)
mongoose.set('useUnifiedTopology', true)
mongoose.set('useNewUrlParser', true)
mongoose.connect(process.env.MONGODB_URI)
mongoose.connection.on('error', err => {
console.error(`%s ${err}`, chalk.red('x'))
})
i18n.configure({
locales: ['en', 'pt'],
directory: `${__dirname}/views/locales`
})
app.use(cors())
app.use(i18n.init)
app.use(express.static(path.join(__dirname, 'public')))
app.set('view engine', 'pug')
app.get('/:lang?', (req, res) => {
res.setLocale(req.params.lang || 'pt')
res.render('index', { GA_TRACKING_ID: process.env.GA_TRACKING_ID })
})
app.use(bodyParser.json())
app.use('/api/', appRoutes)
app.listen(process.env.PORT || 3000, () => {
console.log(
'%s App is running at http://localhost:%d in %s mode',
chalk.green('✓'),
process.env.PORT || 3000,
process.env.NODE_ENV
)
console.log(' Press CTRL-C to stop\n')
})