-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·90 lines (72 loc) · 2.84 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
// require necessary NPM packages
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const cors = require('cors')
// require route files
const clinicRoutes = require('./app/routes/clinic_routes')
const doctorRoutes = require('./app/routes/doctor_routes')
const diseaseRoutes = require('./app/routes/disease_routes')
const sessionRoutes = require('./app/routes/session_routes')
const userRoutes = require('./app/routes/user_routes')
// require database configuration logic
// `db` will be the actual Mongo URI as a string
const db = require('./config/db')
// load secret keys for signing tokens from .env
const dotenv = require('dotenv')
dotenv.config()
// Set the key based on the current environemnt
// Set to secret key base test if in test
if (process.env.TESTENV) {
process.env.KEY = process.env.SECRET_KEY_BASE_TEST
// Set to secret key base development if not test and no key present
// process.env.KEY is present in production and set through heroku
} else if (!process.env.KEY) {
process.env.KEY = process.env.SECRET_KEY_BASE_DEVELOPMENT
}
// require configured passport authentication middleware
const auth = require('./lib/auth')
// establish database connection
mongoose.Promise = global.Promise
mongoose.connect(db, {
useMongoClient: true
})
// instantiate express application object
const app = express()
// set CORS headers on response from this API using the `cors` NPM package
// `CLIENT_ORIGIN` is an environment variable that will be set on Heroku
app.use(cors({ origin: process.env.CLIENT_ORIGIN || 'http://localhost:7165' }))
// define port for API to run on
const port = process.env.PORT || 4741
// this middleware makes it so the client can use the Rails convention
// of `Authorization: Token token=<token>` OR the Express convention of
// `Authorization: Bearer <token>`
app.use((req, res, next) => {
if (req.headers.authorization) {
const auth = req.headers.authorization
// if we find the Rails pattern in the header, replace it with the Express
// one before `passport` gets a look at the headers
req.headers.authorization = auth.replace('Token token=', 'Bearer ')
}
next()
})
// register passport authentication middleware
app.use(auth)
// add `bodyParser` middleware which will parse JSON requests into
// JS objects before they reach the route files.
// The method `.use` sets up middleware for the Express application
app.use(bodyParser.json())
// this parses requests sent by `$.ajax`, which use a different content type
app.use(bodyParser.urlencoded({ extended: true }))
// register route files
app.use(clinicRoutes)
app.use(doctorRoutes)
app.use(diseaseRoutes)
app.use(sessionRoutes)
app.use(userRoutes)
// run API on designated port (4741 in this case)
app.listen(port, () => {
console.log('listening on port ' + port)
})
// needed for testing
module.exports = app