-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
59 lines (48 loc) · 1.77 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
// Copyright (c) 2020 Anastasiia Birillo
const express = require('express');
const mongoose = require('mongoose');
const fileUpload = require('express-fileupload');
const app = express();
const intelLogger = require('intel');
const fs = require('fs');
const multer = require('multer');
const body_parser = require('body-parser');
app.use(body_parser.json({limit: '50mb'}));
app.use(body_parser.urlencoded({extended: true, limit: '50mb'}));
app.use('/uploads', express.static('uploads'));
app.use('/download', express.static('download'));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
next();
});
const dotenv = require('dotenv');
dotenv.config({ path: `${__dirname}/.env` });
// Connect to database
const MONGODB = process.env.MONGODB || 'mongodb://localhost/coding-assistant';
mongoose.connect(MONGODB, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// Set the uploads dir
const uploadsDir = './uploads';
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
const upload = require('./configs/multer_configuration')(multer);
// Set the logger
const loggerDir = './logs';
if (!fs.existsSync(loggerDir)) {
fs.mkdirSync(loggerDir);
}
const logger = intelLogger.getLogger('logger');
logger.addHandler(new intelLogger.handlers.File(loggerDir + '/logs.log'));
const injector = require('./injector');
injector.injectModels();
injector.injectRouters(app, upload);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
logger.info(`${new Date()}: Server is listening on port ${PORT}`);
});