-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
166 lines (111 loc) · 5.11 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use-strict";
const path = require('path');
const express = require('express')
const EXPRESS_APP = express();
const Morgan = require('morgan'); // HTTP request logger
const BodyParser = require('body-parser'); // GET THE CONTENTS OF request.body
const compression = require('compression'); // server response compression
const cors = require('cors'); // this will allow other websites access the api
const ServerError = require('./utils/server-error.js');
const globalErrorHandler = require('./controllers/error-controller.js');
const { _customHeaders } = require('./utils/helpers.js');
// APPEND THE APP BASE DIR. TO THE GLOBAL OBJ.
// Node HAS A GLOBAL NAMESPACE OBJECT CALLED "global"
// ANYTHING THAT YOU ATTACH TO THIS OBJ. WILL BE AVAIL. EVERYWHERE IN YOUR APP
global.__approotdir = __dirname;
// SET THE TEMPLATING ENGINE TO "PUG"
// PUG IS A WHTESPACE SENSITIVE SYNTAX FOR WRITING HTML
EXPRESS_APP.set('view engine', 'pug');
EXPRESS_APP.set('views', path.join(__dirname, 'views'));
// 3RD PARTY MIDDLEWARE > SERVE STATIC FILES
EXPRESS_APP.use(express.static(path.join(__dirname, 'public')));
// 3RD PARTY MIDDLEWARE > IMPLEMENT CORS
// THIS ADDS "Access-Control-Allow-Oriin" TO THE RESPONSE HEADER FOR THE SPECIFIED ORIGINS
EXPRESS_APP.use(cors({
origin:
[
"http://127.0.0.1:1010",
"https://avgmap.herokuapp.com",
"https://avg-dashboard.herokuapp.com",
]
}));
// IF API IS @ api.natours.com; & FRONTEND IS @ natours.com
// TO ENABLE CORS FOR ONLY natours.com >
// EXPRESS_APP.use(cors({
// origin: 'https://www.natours.com'
// }))
// PRE-FLIGHT "OPTIONS" REQUEST RESPONSE(?)
EXPRESS_APP.options('*', cors()); // enable cors pre-flight requests for all routes
// EXPRESS_APP.options('/api/v1/parcelized-agcs/:id', cors()) // enable cors for complex requests (delete, patch, post) only on this specific route
// 3RD PARTY MIDDLEWARE > PARSE DATA FROM INCOMING REQUESTS
EXPRESS_APP.use(BodyParser.json({limit: '16mb'}))
// 3RD PARTY MIDDLEWARE > REQ. LOGGING
// perform logging only while in development mode..
if (process.env.NODE_ENV === 'development') {
// console.log(highlight(`Our node environment is currently: ${process.env.NODE_ENV} `))
EXPRESS_APP.use(Morgan('dev'))
};
// CUSTOM MIDDLEWRE EXAMPLE #1
// MW. IS PART OF THE REQ, RES CYCLE
// MUST BE DEFINED BEFORE ALL THE ROUTE HANDLERS (OR ROUTERS) BELOW
// OTHERWISE IT DOESN'T WORK because the routes WOULD terminate the req, res cycle BEFORE MW. RUNS
EXPRESS_APP.use((request, response, next) => {
// console.log('Hello from the 1st (custom) middleware in app.js..');
// IMPORTANT -> Initialize `request.locals` property
request.locals = {};
next();
});
// CUSTOM MIDDLEWRE EXAMPLE #2
// MANIPULATE THE REQUEST OBJ. >> THIS ADDS A CUSTOM PROPERTY TO THE REQUEST OBJ.
EXPRESS_APP.use((request, response, next) => {
request.requestTime = new Date().toISOString(); // add a new custom property ('requestTime') to the req. obj.
next();
});
// SERVER RESPONSE COMPRESSION MIDDLEWARE FOR ALL TEXT SENT TO CLIENTS
EXPRESS_APP.use(compression());
// DISABLE DEFAULT HEADERS
EXPRESS_APP.use((req, res, next) => {
return _customHeaders(EXPRESS_APP, req, res, next);
});
// LOAD THE ROUTERS
const parcelizedAgcsRouter = require('./routes/parcelized-agc-routes.js');
const agcsRouter = require('./routes/agc-routes.js');
const usersRouter = require('./routes/user-routes.js');
const viewRouter = require('./routes/view-routes.js');
const geoClustersRouter = require('./routes/geo-cluster-routes.js');
const geofilesRouter = require('./routes/geofile-routes.js');
const legacyAgcsRouter = require('./routes/legacy-agc-routes.js');
const pmroRouter = require('./routes/pmro-routes.js');
const farmProgramRouter = require(`./routes/clustered-farm-program-routes.js`);
const farmersRouter = require(`./routes/farmer-routes.js`);
// MOUNTING THE ROUTERS
EXPRESS_APP.use('/', viewRouter);
EXPRESS_APP.use('/api/demo/', viewRouter);
EXPRESS_APP.use('/api/v1/agcs/', agcsRouter);
EXPRESS_APP.use('/api/v2/geo-clusters/', geoClustersRouter);
EXPRESS_APP.use('/api/v1/parcelized-agcs/', parcelizedAgcsRouter);
EXPRESS_APP.use('/api/v1/users/', usersRouter);
EXPRESS_APP.use('/api/v2/geofiles/', geofilesRouter);
EXPRESS_APP.use('/api/v2/legacy-agcs/', legacyAgcsRouter);
EXPRESS_APP.use(`/api/v2/pmros/`, pmroRouter);
EXPRESS_APP.use(`/api/v3/clustered-farm-programs/`, farmProgramRouter)
EXPRESS_APP.use(`/api/v3/farmers/`, farmersRouter)
// HANDLE ALL NON PRE-DEFINED ROUTES
EXPRESS_APP.use('*', (req, res, next) => {
res.status(404).json({
status: 'fail',
message: `Can't find ${req.originalUrl} on this server.`
});
// const err = new Error(`Can't find ${req.originalUrl} on this server.`);
// // DEFINE CUSTOM PROPS. ON err
// err.status = `fail`;
// err.statusCode = 404;
// // ANY ARG. PASSED TO next() IS ASSUMED BY EXPRESS TO BE AN ERROR.
// next(err)
// next(new ServerError(`Can't find ${req.originalUrl} on this server.`, 404))
});
// GLOBAL ERROR HANDLING M.WARE
EXPRESS_APP.use(globalErrorHandler);
// ?????
EXPRESS_APP.use(express.urlencoded({ extended: true}));
module.exports = EXPRESS_APP;