-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
52 lines (41 loc) · 1.25 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
// Load environment variables
require('dotenv-safe').config({
allowEmptyValues: true,
})
const express = require('express')
const compression = require('compression')
const bodyParser = require('body-parser')
const app = express()
const cors = require('cors')
const routes = require('./routes')
const logger = require('./utils/logger')
// Allow cors in development
if (process.env.NODE_ENV === 'development') {
app.use(cors())
}
// Compress all responses
app.use(compression())
// Parse the form data (application/x-www-form-urlencoded) and expose to req.body
app.use(bodyParser.urlencoded({ extended: true }))
// Parse the json data (application/json) and expose to req.body
app.use(bodyParser.json())
// Routes
app.use('/api', routes)
app.get('/', (req, res) => {
res.status(404).send('Welcome to BirdsEye!')
})
// Any undefined route
app.use((req, res, next) => {
res.status(404).send('Page not found!')
})
// Error handler
app.use((err, req, res, next) => {
logger.error(err.stack)
res.status(500).send('Internal server error!')
})
// Listen for requests
const server = app.listen(process.env.PORT, process.env.IP, () => {
logger.info(`Server started on ${process.env.IP}:${process.env.PORT}`)
})
// Export server for testing
module.exports = server