-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (104 loc) · 3.23 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
import express from 'express';
import bodyParser from 'express';
import fs from 'fs/promises';
import logger from 'morgan';
import multer from 'multer';
import path from 'path';
import servicesMiddleware from './lib/common/middlewares/services.js';
import tokenChecker from './lib/common/middlewares/tokenChecker.js';
import indexRouter from './lib/common/routes.js';
import removeNull from "./lib/common/utils/stripedJSON.js";
const __dirname = path.resolve();
global.appRoot = path.resolve(__dirname);
const storageAvatar = multer({dest: path.join(global.appRoot, 'assets/public/uploads/')});
const storageVoiceConfig = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(global.appRoot, 'config/speech/'));
}, filename: function (req, file, cb) {
cb(null, 'LISA-gfile.json');
}
})
});
const app = express();
app.use(logger('dev'));
app.use(servicesMiddleware());
app.use(express.json());
app.use(function (req, res, next) {
res.json = function (data) {
const stripedData = JSON.stringify(removeNull(data));
// content-type
if (!this.get('Content-Type')) {
this.set('Content-Type', 'application/json');
}
return this.send(stripedData);
}.bind(res);
next();
});
app.use(bodyParser());
app.use(express.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'assets/public')));
app.use('/avatar', express.static(path.join(__dirname, 'assets/public/uploads')));
app.use(function (req, res, next) {
res.setHeader('X-Powered-By', 'L.I.S.A.');
next();
});
app.use(tokenChecker);
app.use(function (req, res, next) {
req.storageAvatar = storageAvatar;
req.storageVoiceConfig = storageVoiceConfig;
next();
});
app.use('/api/v1', indexRouter);
app.get('/', function (req, res) {
res.send('It works!');
});
app.use(function (req, res, next) {
res.status(404).json({
errorCode: '404',
message: 'Sorry can\'t find that!',
});
});
app.use(function (err, req, res, next) {
if (err.name === 'ServiceError') {
res.status(err.status).json({
errorCode: err.errorCode,
message: err.message,
error: err.error,
});
}
else if (err.name === 'SequelizeValidationError' || err.name === 'SequelizeUniqueConstraintError') {
err.name = err.name.replace('Sequelize', '');
console.error(err);
res.status(400).json({
errorCode: '400',
message: 'Payload is incorrect!',
error: err,
});
}
else {
console.error(err);
res.status(500).json({
errorCode: '500',
message: 'An error as occurred!',
error: err,
});
}
});
export default app;
// Add method to strings
String.prototype.toCamelCase = function () {
const regex = new RegExp(/(?:_|-)(.)/g);
if (regex.test(this)) {
return this.toLowerCase().replace(regex, (match, group1) => {
return group1.toUpperCase();
});
}
return this + '';
};
// Add method exists to fs promises
fs.exists = function (file) {
return fs.access(file, fs.F_OK)
.then(() => true)
.catch(() => false)
}