forked from afuh/rick-and-morty-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
53 lines (38 loc) · 1.28 KB
/
server.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
require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const path = require('path')
const morgan = require('morgan')
const app = express()
const api = require('./routes/api')
const errors = require('./handlers/errors')
const db = process.env.NODE_ENV === "production" ? process.env.DATABASE : 'mongodb://localhost:27017/rickmorty-api'
mongoose.connect(db)
mongoose.Promise = global.Promise
mongoose.connection.on('error', err => {
console.error(`→ ${err.message}`)
})
if (app.get('env') !== 'test') {
app.use(morgan('dev', {
skip(req) {
return req.path.match(/(ico|png|svg|jpeg|woff2|css|js|txt|)$/ig)[0] ? true : false
}
}))
}
app.set('trust proxy', 1)
app.use(express.static(path.join(__dirname, 'static')))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.get('/', (req, res) => res.redirect('/api'))
app.use('/api', api)
app.use(errors.notFound)
app.use(errors.productionErrors)
const PORT = process.env.PORT || 8080
app.listen(PORT, () => console.log('\x1b[34m%s\x1b[0m', `
${app.get('env').toUpperCase()}
Port → http://localhost:${PORT}
Database → ${mongoose.connection.host}/${mongoose.connection.name}
`
))
module.exports = app