-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·117 lines (100 loc) · 3.28 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
global.express = require('express');
const app = express();
const morgan = require('morgan');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const http = require('http');
const https = require('https');
const bodyParser = require('body-parser');
const moment = require('moment');
mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const constants = require('./config/constants');
const keys = require('./keys/keys');
const commonFunction = require('./helper/commonFunction.helper');
const Lang = require('./helper/response.helper');
const port = process.env.PORT || keys.PORT || 2020; // setting port
const env = process.env.ENV || 'development'; //setting environment
const httpsAllow = process.env.HTTPS_ALLOW || keys.HTTPS_ALLOW; //setting https or http server
require('./database/mongoose.js'); // for database connection
//make default folders and copy files to relative folder
require('./services/makeFolders/makeFolders.service');
app.use(cors()); // for allow all request
app.use(bodyParser.urlencoded({ extended: true })); // to support URL-encoded bodies and to remove deprecation warnings
app.use(bodyParser.json()); // to parse body in json
app.use(express.static(path.join(__dirname, 'public'))); // to set public path
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(morgan('dev'));
//BASE_URL configuration
// app.locals.base_url = keys.BASE_URL + ':' + keys.PORT;
//Global BASE_URL
// global.app_base_url = keys.BASE_URL + ':' + keys.PORT;
// version 1
app.use('/api/v1/product', require('./v1/routes/product.route'));
app.get('/', (req, res) => {
res.send(`
<html>
</head>
<title> Test API </title>
</head>
<body>
<h1 align="center">It works!</h1>
<p align="center">Welcome to Test Project</p>
<p align="center">This is the default web page for this server.</p>
</body>
</html>
`)
})
//catch 404 and forward to error handler
app.use(function(req, res, next) {
// console.log(req.header.lang);
var err = new Error('Not Found');
err.status = 404;
next(err);
});
//setting for error message in environment
if (env == 'development') {
app.use((err, req, res, next) => {
console.log(err);
res.status(err.status || 500).send({
status:constants.STATUS_CODE.FAIL,
message: err.message,
error: true,
e: err
});
});
} else {
// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
console.log(err);
res.status(err.status || 500).send({
message: Lang.responseIn("GENERAL.GENERAL_CATCH_MESSAGE", req.headers.lang),
error: true,
e: null
});
});
}
if (httpsAllow) {
//Global BASE_URL
global.app_base_url = keys.BASE_URL;
http.createServer(app).listen(port, () => {
console.log(
`Server started with https on ${env} envrionment on port ${port}`
);
});
} else {
//Global BASE_URL
global.app_base_url = keys.BASE_URL + ':' + keys.PORT;
http.createServer(app).listen(port, () => {
console.log(
`Server started with http on ${env} envrionment on port ${port}`
);
});
/*app.listen(port,()=>{
console.log(`Server is listening on port ${port} with ${env} environment`);
});*/
}
exports = module.exports = app;