-
Notifications
You must be signed in to change notification settings - Fork 88
/
server.js
65 lines (51 loc) · 1.84 KB
/
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
#!/usr/bin/env node
// eslint-disable-next-line strict
require('dotenv').config();
const cors = require('cors');
const argv = require('minimist')(process.argv.slice(2));
const express = require('express');
const bodyParser = require('body-parser');
const gzipProcessor = require('connect-gzip-static');
// const updateNotifier = require('update-notifier'); commeting this as its dependents have vulnarablities
const dataAccessAdapter = require('./src/db/dataAccessAdapter');
const databasesRoute = require('./src/routes/database');
const authMiddleware = require('./src/controllers/auth');
// notify users on new releases - https://github.com/arunbandari/mongo-gui/issues/5
// const pkg = require('./package.json');
// updateNotifier({ pkg }).notify();
// initialize app
const app = express();
// middleware for simple authorization.
app.use(authMiddleware.auth);
// serve static files form public
app.use(express.static('public'));
// process gzipped static files
app.use(gzipProcessor(__dirname + '/public'));
// enables cors
app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json({ limit: process.env.BODY_SIZE || '50mb' }));
// api routing
app.use('/databases', databasesRoute);
// serve home page
app.get('/', (req, res) => res.sendFile(__dirname + '/public/index.html'));
// connect to database
dataAccessAdapter.InitDB(app);
// listen on :port once the app is connected to the MongoDB
app.once('connectedToDB', () => {
const port = argv.p || process.env.PORT || 4321;
app.listen(port, () => {
console.log(`> Access Mongo GUI at http://localhost:${port}`);
});
});
// error handler
app.use((err, req, res, next) => {
console.log(err);
const error = {
errmsg: err.errmsg,
name: err.name,
};
return res.status(500).send(error);
});