-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (45 loc) · 1.51 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
57
58
59
60
var config = require('./config')
// Express and Middleware
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var logger = require('morgan');
// swagger
var swaggerUi = require('swagger-ui-express')
var YAML = require('yamljs')
var swaggerDocument = YAML.load('./swagger/swagger.yaml')
// Router
var apiRouter = require('./routes/api')
// MongoDB Connection
var mongoose = require('mongoose')
var mongodbUri = config.mongodb.uri
var mongodbOptions = config.mongodb.options
mongoose.connect(mongodbUri, mongodbOptions)
var conn = mongoose.connection
conn.on('error', function(error) {
throw error;
})
// conn.once('open', function() {
// console.log('Connected to mongodb')
// })
var port = process.env.PORT || 3001
if (process.env.NODE_ENV === 'production') app.use(logger('common'))
else if (process.env.NODE_ENV === 'development') app.use(loggger('dev'))
app.all('/*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept')
res.header('Access-Control-Allow-Methods', '*')
next()
})
// app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static('public'))
app.get('/', function(req, res) {
res.sendFile('index.html')
})
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
app.use('/api/todo', apiRouter)
app.listen(port, function() {
console.log('Express app running on port', port)
})
module.exports = app