-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·163 lines (130 loc) · 4.53 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
const express = require('express');
const mongoose = require('mongoose');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const http = require('http');
const socketIO = require('socket.io');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const passport = require('passport');
const path = require('path');
const swaggerJsDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
// initialize the app
const app = express();
global.appRoot = path.resolve(__dirname);
const PORT = process.env.PORT || 4000;
// Creating a Server
const server = http.createServer(app);
// Start Server using environment port
server.listen(PORT);
// Configuring the database
const Config = require('./src/config/mongodb.config');
mongoose.Promise = global.Promise;
// mongo configuration
mongoose.set('useCreateIndex', true);
mongoose.set('useNewUrlParser', true);
mongoose.set('useUnifiedTopology', true);
mongoose.set('useFindAndModify', true);
// Connecting to the database
mongoose.connect(Config.url)
.then((data) => {
// console.log(data);
console.log("Successfully connected to MongoDB.");
}).catch(err => {
console.log('Could not connect to MongoDB.');
process.exit();
});
//Socket Connection
let io = socketIO(server);
io.on('connection', (socket) => {
console.info('a new user has connected')
socket.on('message', (msg) => {
io.emit('message', msg);
});
socket.on('disconnect', (socket) => {
console.info('a user has disconnected');
});
});
// defining the Middleware
app.use(cors());
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
// useTempFiles: true,
// tempFileDir: '/tmp/'
}));
// Body Parser
app.use(express.json({ limit: '30mb' })); // Body limit is 30Mb
// Set the static folder
app.use('/public', express.static(path.join(__dirname, '../public')))
// Bodyparser Middleware
// app.use(bodyParser.json({ limit: '10kb' }));
// Helmet
app.use(helmet());
// Rate Limiting
const limit = rateLimit({
max: 1000, // max requests
windowMs: 60 * 60 * 1000, // 1 Hour of 'ban' / lockout
message: 'Too many requests' // message to send
});
// app.use('/api', limit); // Setting limiter on specific route
// Data Sanitization against NoSQL Injection Attacks
app.use(mongoSanitize());
// Data Sanitization against XSS attacks
app.use(xss());
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
console.log('working')
// require('./config/authguard.config.js')(passport);
require('./src/routes/customers.routes')(app);
require('./src/routes/brands.routes')(app);
require('./src/routes/branch.routes')(app);
require('./src/routes/category.routes')(app);
require('./src/routes/brands.routes')(app);
require('./src/routes/blogs.routes')(app);
require('./src/routes/users.routes')(app);
require('./src/routes/products.routes')(app);
require('./src/routes/gallery.routes')(app);
require('./src/routes/website.routes')(app);
// Api Documentation Setup
const swaggerOptions = {
swaggerDefinition: {
info: {
title: 'E-Commerce Api',
description: 'Complete E-Commerce and Inventory Api',
contact: {
name: 'Harmony Alabi',
},
servers: ["http://localhost:" + PORT]
},
},
apis: ["./routes/*.routes.js"]
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Handler for 404 - Resource Not Found
app.use((req, res, next) => {
res.status(404).send({ message: 'We think you are lost!' });
});
// Handler for Error 500
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send({ message: 'Internal Server error!' });
// res.sendFile(path.join(__dirname, '../public/500.html'))
});
// // Create a Server
// var server = app.listen(PORT, function() {
// var host = server.address().address
// var port = server.address().port
// console.log("App listening at http://%s:%s", host, port)
// });
// var io = require('socket.io')(server);
// io.on('connection', function(socket) {
// socket.on('message', function(msg) {
// io.emit('message', msg);
// });
// });