-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
86 lines (71 loc) · 2.36 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
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
var express = require('express')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
var methodOverride = require('method-override')
var routes = require('./routes')
const passport = require('passport')
var env = process.env.NODE_ENV || 'development';
var config = require('./config/config')[env];
var swaggerJSDoc = require('swagger-jsdoc')
var swaggerUi = require('swagger-ui-express')
var neo4jSessionCleanup = require('./middlewares/neo4jSessionCleanup')
var writeError = require('./helpers/response').writeError
require('./middlewares/jwt')
var app = express()
var api = express()
app.use(config.api_path, passport.authenticate('jwt', { session: false }), api)
var swaggerDefinition = {
info: {
title: 'Company API (Node/Express)',
version: '1.0.0',
description: 'API information about companies.'
},
host: 'localhost:3000',
basePath: '/'
}
// options for the swagger docs
var options = {
// import swaggerDefinitions
swaggerDefinition: swaggerDefinition,
// path to the API docs
apis: ['./routes/*.js']
}
// initialize swagger-jsdoc
var swaggerSpec = swaggerJSDoc(options)
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))
// serve swagger
api.get('/swagger.json', function (req, res) {
res.setHeader('Content-Type', 'application/json')
res.send(swaggerSpec)
})
// set api setup
if (process.env.NODE_ENV !== 'test') {
api.use(logger('dev'))
}
api.use(express.json())
api.use(cookieParser())
api.use(express.urlencoded({ extended: false }))
api.use(methodOverride())
// enable CORS
api.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Credentials', 'true')
res.header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
next()
})
// api custom middlewares:
api.use(neo4jSessionCleanup)
// api routes
api.get('/companies', routes.companies.list)
api.post('/companies', routes.companies.create)
api.put('/companies', routes.companies.update)
api.get('/companies/:companyId', routes.companies.getCompanyById)
api.delete('/companies/:companyId', routes.companies.remove)
// api error handler
api.use(function (err, req, res, next) {
if (err && err.status) {
writeError(res, err)
} else next(err)
})
module.exports = app