-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (58 loc) · 2.19 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
//server.js
//BASE SETUP
// =============================================================================================================================
var express = require('express')
var cors = require('cors')
var app = express()
process.env.NODE_ENV = app.get('env') //set node env
var environment = require('./config/environment') // get our config file
var db = require('./config/db')
var routesSetup = require('./config/routes')
var jwtHelper = require('./lib/jwthelper')
//Configure app to user bodyParse()
//this will let use get the data from a post
var bodyParser = require('body-parser')
// app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
app.use(bodyParser.json({type: 'application/vnd.api+json'}))
// // parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))
// // parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.api+json' }))
app.use(cors());
db.setupDatabase()
app.set('superSecret', environment.secret) // secret variable
//Block secret urls midlleware
app.use(function(req, res, next){
// Website you wish to allow to connect
// res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
// res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
// res.setHeader('Access-Control-Allow-Headers', 'content-type');
// console.log(req.body)
// Website you wish to allow to connect
//res.setHeader('Access-Control-Allow-Origin', '*');
var isUserPostRoute = ((req.path.indexOf('users') > -1 && req.method === 'POST') || req.path.indexOf('auth') > -1)
if (!isUserPostRoute) {
jwtHelper.verifyJsonWebToken(req,res,next,app)
} else {
next()
}
})
var morgan = require('morgan')
app.use(morgan('dev'))
routesSetup.setupRoutesAndVersions(app)
// app.use(function (err, req, res, next) {
//
// // if(err) {
// // return res.send(err.message)
// // }
// next()
// })
module.exports = app
//Service setup
var port = process.env.PORT || 8080
app.listen(port,function() {
console.log('Now is running on port' + port)
})