-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (40 loc) · 1.27 KB
/
index.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
require('dotenv').config()
const Koa = require('koa')
const cors = require('kcors')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const koaBody = require('koa-body')
const logger = require('./middlewares/logger')
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const app = new Koa()
const router = new Router()
const environment = process.env.NODE_ENV || 'development'
const port = process.env.PORT || 8080
require('./routes/filter')(router)
require('./routes/iot')(router)
router.get('/', async (ctx, next) => {
ctx.body = 'Super API'
})
if (environment === 'production') {
console.log('Initializing helmet...')
const helmet = require('koa-helmet')
app.use(helmet())
}
app.use(cors())
app.use(koaBody({multipart: true}))
app.use(bodyParser())
app.use(logger)
app.use(router.routes()).use(router.allowedMethods())
mongoose.connect(process.env.DATA_BASE, function (err, res) {
if (err) {
console.log('ERROR connecting to: ' + process.env.DATA_BASE + '. ' + err)
process.exit(1)
}
console.log('Succeeded connected to: Mongo DB')
app.listen(port, function () {
console.log(`Run in ${process.env.NODE_ENV || 'develepment'} mode`)
console.log(`Server: http://localhost:${port}`)
})
})
module.exports = app