-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
40 lines (30 loc) · 1.02 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
'use strict'
const express = require('express')
const log = require('./tools/logger')
const bodyParser = require('body-parser')
const db = require('./db')
// midleware
const catchErrors = require('./middleware/catchErrors')
const { preJson } = require('./middleware/pre')
const categoryControllers = require('./lib/index')
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
const envs = ['production', 'development', 'test']
if (envs.indexOf(process.env.NODE_ENV) < 0) {
throw new Error({ 'Environment error': 'Env not set' })
}
app.get('/', (req, res) => {
res.send({ status: 'Online', hey: req.protocol + '://' + req.get('host') + req.originalUrl })
})
app.use('/', preJson, categoryControllers)
db.init(function (error) {
if (error) { throw error }
log.fail('Database connection Fail !')
})
// error handler
app.use(catchErrors)
app.listen(process.env.NODE.ENV || 3000, function (error) {
if (error) throw error
log.info(`Listening on ${process.env.NODE.ENV || 3000}`)
})