-
Notifications
You must be signed in to change notification settings - Fork 38
/
server.js
96 lines (77 loc) · 2.99 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
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
'use strict'
// Set up ======================================================================
let http = require('http')
let express = require('express')
let bodyParser = require('body-parser')
let cookieParser = require('cookie-parser')
let favicon = require('serve-favicon')
let methodOverride = require('method-override')
let logger = require('morgan')
let passport = require('passport')
// Will automatically load index.js in routes folder
let routes = require('./app/routes')
let app = express()
const ENV = require('./config/env')[process.env.NODE_ENV || 'development']
require('./config/passport')(passport) // pass passport for configuration
// Set a static folder used by express. This folder contains our Angular application
// Take a look at: expressjs.com/en/starter/static-files.html
console.log(__dirname);
app.use(express.static(__dirname + '/public'));
// Set logs
app.use(logger('combined'));
// https://www.npmjs.com/package/serve-favicon
// Uncomment after placing your favicon in /public:
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
// Set parser to get the body data request
// ref: https://www.npmjs.com/package/body-parser
app.use(bodyParser.urlencoded({
'extended': 'true'
}))
app.use(bodyParser.json())
app.use(bodyParser.json({
type: 'application/vnd.api+json'
}));
// Set parser for cookies, same logic as bodyParser but for cookies
// Has some extra options, take a look at: https://www.npmjs.com/package/cookie-parser
app.use(cookieParser())
// Override HTTP methods to support DELETE PUT, if client device doesn't support them
app.use(methodOverride('X-HTTP-Method-Override'))
// Initialize passport used by express for authentication
app.use(passport.initialize())
//Load all api routes
//Pass passport to routes
app.use('/api', routes(passport))
// Connect to mongodb
let mongoose = require('mongoose')
mongoose.connect(ENV.db);
// Catch 404 not found errors and forward to error handler
app.use((request, response, next) => {
let err = new Error('Not Found')
console.log("Were here")
err.status = 404
next(err)
})
// To better understand middlewares:
// http://expressjs.com/en/guide/writing-middleware.html
// Middleware to catch all errors
app.use((error, request, response, next) => {
console.error(error.stack)
console.log("Were also here")
response.status(error.status || 500).send(error.message)
})
//Export function startServer with port, path and callback params it's used by brunch
// brunch? -> https://www.npmjs.com/package/brunch
// Brunch is not used here but it is good to know it
exports.startServer = (port, path, callback) => {
// Create server
let server = http.Server(app);
// Listening
port = process.env.PORT || port
server.listen(port, callback)
console.log(`server listening on port ${port}`)
//Intercept when application killed
process.on('SIGINT', function() {
console.log("\nStopping...")
process.exit()
});
}