-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsponsorship_server.js
96 lines (76 loc) · 2.77 KB
/
sponsorship_server.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
// Third party imports
require('dotenv').config({ path: './config.env' });
const express = require('express');
const bodyParser = require('body-parser');
// const upload = require('multer')();
const sqlite = require('sqlite3');
const cors = require('cors');
// node inbuilt package imports
const path = require('path');
// filesystem imports
const models = require('./models'); // import all the models
const errorHandler = require('./middleware/error');
// Creating an express app
const app = express();
global.__basedir = __dirname;
// Configuring the views folder using which the controllers serve the webpages
app.set("views", path.join(__dirname, "views"));
// By setting view engine here, there is no need to mention file extension again in controllers
app.set('view engine', 'ejs');
const corsOptions = {
"origin": ['http://localhost:3000', '*'],
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204,
"credentials": true
}
app.use(cors(corsOptions));
// Setting up the static files
app.use(express.static(path.join(__dirname, "public")));
// Adding a json middleware for parsing application/json data
app.use(express.json());
// For parsing application/xwww-form-urlencoded data
app.use(bodyParser.urlencoded({ extended: true }));
// For parsing multipart/form-data
// app.use(upload.array());
// Adding express-session middleware
// Setting port as a key 'port' to the app
app.set('port', process.env.PORT || 5500);
// Adding user to the response locals.
// So that, it can be accessible in the other routes
/*
app.use(async (request, response, next) => {
const { userId } = request.session;
if (userId) {
await models.User.findOne({ where: { id: userId } }).then((user) => {
response.locals.user = user;
});
}
next();
});
*/
// Setting up the routes
app.use('/', require('./routes/main'));
// app.use('/admin', require('./routes/admin/main'));
app.use('/api', require('./routes/api/main'));
// 404 middleware
app.use((request, response, next) => {
response.status(404)
.send("Oops! The page you are looking for doesn't exist");
next();
});
// Error Handler (Should be last piece of middleware)
app.use(errorHandler);
// Listening at the port set before
app.listen(app.get('port'), () => {
// Creates all the tables required, if not exist
models.sequelize.sync().then(() => {
// Logging after promise resolve
console.log('DB sequelized\n');
}).catch((err) => {
// loggin if there is any error while sequelizing
console.log(err.message);
});
// Loggin the server host name and port
console.log(`Server Running at http://${(process.env.NODE_ENV === 'production') ? '172.105.49.237' : 'localhost'}:${app.get('port')}/`)
});