-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
91 lines (68 loc) · 1.99 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
87
88
89
90
91
require("dotenv")
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const morgan = require('morgan');
const errorHandler = require('./src/middlewares/errorHandler');
const unhandledRequests = require('./src/middlewares/unhandledRequests')
const authenticateToken = require("./src/middlewares/authenticateToken")
const { unless } = require("./src/helpers")
// Using middlewares to all the requests
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(morgan('dev'))
/*
Using Cross Origin middelware
**/
app.all('/*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
return res.status(200).json({});
}
next();
});
/*
Importing Routes
**/
const routes = require("./src/routes")
/*
Using Routes
**/
app.use("/api", routes)
/**
* Static
*/
const _clientDir = "client/dist/ng/browser";
app.use('/', express.static(path.resolve(__dirname, `${_clientDir}/`)));
/**
* Spa Res Sender
*/
const renderIndex = (req, res) => {
res.sendFile(path.resolve(__dirname, `${_clientDir}/index.html`));
};
/**
* Prevent server routing and use @ng2-router.
*/
app.get(unless('/api-docs'), renderIndex);
/**
* API Documentation with Swagger.
*/
const swaggerUi = require("swagger-ui-express"),
YAML = require("yamljs")
swaggerDocument = YAML.load("./swagger.yaml");
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// Returning a 404 not found error for unsupported endpoints
app.use(unhandledRequests);
app.use(errorHandler)
module.exports = app;