-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
129 lines (95 loc) · 2.88 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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
var path = require('path');
const http = require('http');
const bcrypt = require("bcryptjs");
var env = process.env.NODE_ENV || 'dev';
// start server in production
/* if (env == 'production') {
http.createServer(app);
app.listen();
} */
// start server in dev mode
// const port = process.env.PORT || 3000;
const port = process.env.PORT || 38080;
http.createServer(app);
app.listen(port, () => {
console.log(`Server Running in ${port}`);
});
// end
console.log(env);
if (env == 'production') {
global.apiUrl = "https://www.pay2mate.com/api";
global.APPURL = "https://www.pay2mate.com/";
/* global.apiUrl = "https://pay2mate.herokuapp.com/api";
global.APPURL = "https://pay2mate.herokuapp.com/"; */
}
if (env == 'dev') {
global.apiUrl = "http://localhost:3000/api";
global.APPURL = "http://localhost:3000/";
}
global.ROOT = __dirname;
// // view engine setup
// app.set('view engine', 'ejs');
//--------------our db instance------------------
const sequelize = require("./util/database");
const sequelizeRealtions = require("./util/table-relations");
const User = require('./models/user');
// ------------------------------------------------
// ______our routes________
const apiRoutes = require("./routes/api");
//___________________________
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// cors origin solved
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, Content-Length, X-Requested-With, cache-control"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET,POST,PATCH,DELETE,OPTIONS,PUT"
);
next();
})
app.use('/uploads',express.static(path.join(__dirname, './uploads')));
// access frontend folder
app.use('/',express.static(path.join(__dirname, '/angular')));
/* app.use(function(req, res, next){
res.on('finish', function(){
console.log("Finished " + res.headersSent); // for example
console.log("Finished " + res.statusCode); // for example
// Do whatever you want
});
next();
}) */
app.use('/api', apiRoutes);
// access angular page according to routes
app.use((req,res) => {
res.sendFile(path.join(__dirname,"angular", "index.html"));
})
// add tables realtion
sequelizeRealtions.allTableRealtions();
sequelize
.sync({alter: false, force: false})
.then(result => {
console.log('table created');
/* sequelizeRealtions.seedDatabase();
return bcrypt.hash('admin123',10)
.then(hash => {
User.create({
uniqueName: 'admin',
email: 'admin@pay2mate.com',
password: hash,
isVerified: true, // email
isAdmin: true,
trustedUser:true // kyc
});
}) */
}).catch(err => {
console.log("error occured in db" + err);
});
module.exports = app;