forked from simdo/rest.api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (98 loc) · 3 KB
/
index.js
File metadata and controls
117 lines (98 loc) · 3 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// import packages that we need
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
// import our api routes
const index = require('./routes/index')
const user = require('./routes/user')
// define base uri with api version protocol defined in package.json
const env = require('./env.json')
// define app
var app = express()
// [Use native promises for backwards compatibility. Mongoose 4 returns mpromise promises by default.](http://mongoosejs.com/docs/promises.html)
mongoose.Promise = Promise
// create reusable mongo connect func
var mongoConnect = function() {
mongoose.connect(env.mongodb || 'mongodb://localhost/restapi', { server: { auto_reconnect: true } })
}
// get mongoose connection instance
var db = mongoose.connection
db.once('open', function() {
console.log('MongoDB connection opened!')
})
db.on('connected', () => {
console.info('Connected to MongoDB!')
})
db.on('reconnected', function () {
console.log('MongoDB reconnected!')
})
db.on('error', function(error) {
console.error('Error in MongoDb connection: ' + error)
mongoose.disconnect()
})
db.on('reconnected', function () {
console.log('MongoDB reconnected!')
})
db.on('disconnected', function() {
console.log('MongoDB disconnected!')
setTimeout(mongoConnect, 10000)
})
// connect to local mongodb by default if it not specified in env
mongoConnect()
// remove x-powered-by header variable
app.disable('x-powered-by')
// ? app.disable('etag');
// This function is executed every time the app receives a request and check
// mongodb readyState flag: 0 = disconnected, 1 = connected, 2 = connecting,
// 3 = disconnecting
app.use(function (req, res, next) {
if (db.readyState != 1) {
var err = new Error('Database connection problem')
err.status = 500
next(err)
}
else
next()
})
// parse application/json
app.use(bodyParser.json())
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// set 3000 port as default if it not specified in env
app.set('port', env.port || 3000)
// register api routes
app.use('/' + env.api.version, index)
app.use('/' + env.api.version + '/user', user)
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found')
err.status = 404
next(err)
})
// error handlers
// development error handler
// will print stacktrace
if (env.environment === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
})
})
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
})
})
// start our rest api server
var server = app.listen(app.get('port'), function() {
console.log('RESTful API server listening on port ' + server.address().port)
})
// prevent too long threshold
server.timeout = 2048